-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem comparing 2 datetimes, DB = MySql
PostPosted: Thu Aug 06, 2009 6:19 pm 
Newbie

Joined: Tue May 19, 2009 11:47 am
Posts: 4
Hello,

My app is executing a query wrote using the hibernate API for Java.
The query has a SELECT and it has a comparison: os.fecha <= :fechaFinal
os.fecha is a "datetime" in the database.
fechaFinal is a java.util.Date

The code is here:

Code:
    public List<OrdenServicioConPaciente>
    consultarOServConSaldoPendientes(Date fechaInicial, Date fechaFinal)
    {
       Session session = null ;
       SessionFactory sessionFactory = null ;
       Transaction tx = null ;
       List result = null ;
        Object[] datosFila = null ;       
        OrdenServicioConPaciente ordenServicioConPaciente = null ;
        ArrayList<OrdenServicioConPaciente> listRetorno = null ;
        try {
           sessionFactory  = HibernateUtil.getSessionFactory();
           session = sessionFactory.getCurrentSession();
       }
       catch (java.lang.Exception exc) {
          exc.printStackTrace();
          
       }
       tx = session.beginTransaction();
       Query query =
            session.createQuery("SELECT os.fecha, os.id, os.saldo, pac.tipoDocumento , pac.numDocumento, pac.primerNombre, pac.segundoNombre, pac.primerApellido, pac.segundoApellido FROM OrdenServicio os, Paciente pac WHERE os.saldo > 0.1 AND os.fechaPagoSaldoPaciente IS null AND os.fecha <= :fechaFinal AND os.pacientesId=pac.id ORDER BY os.fecha DESC");
        // getByDocId.setDate("fechaInicial", fechaInicial );
        query.setDate("fechaFinal"  , fechaFinal );       
        result = (List)query.list();


When the final date (i.e. fechaFinal) is '2009-08-05 23:59:59' result has 62 elements.
When i execute the SQL sentence from MySQL Query Browser then 76 rows are fetched.

In particular, in java code there are 2 rows with FECHA column having values '2009-08-05 14:56:34' (first row) and '2009-08-05 14:37:30' (second row) that must be in the result list but they are not there. What is the problem ?

I'm working with MySQL Server v5.1

Any help or insight is really appreciated.

Ricardo De la Rosa


Top
 Profile  
 
 Post subject: Re: Problem comparing 2 datetimes, DB = MySql
PostPosted: Thu Aug 06, 2009 7:01 pm 
Newbie

Joined: Tue May 19, 2009 11:47 am
Posts: 4
Hi,

I changed the code:
Code:
query.setDate("fechaFinal", fechaFinal);

with:
Code:
java.util.Calendar calendarFechaFinal = null ;
// initialization of calendarFechaFinal
query.setCalendar("fechaFinal", calendarFechaFinal);


and it begins to run succesfully :)

Best regards,
Ricardo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.