-->
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.  [ 4 posts ] 
Author Message
 Post subject: Searching a record By date
PostPosted: Wed Apr 13, 2005 4:10 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi I am doing a simple test when storing an Object in by MySQL DB and retrieving it at the same time. I am using Spring 2.1 RC1, Hibernate 3.0 and Eclipse 3.1M5, MySql 4.

When I start the test there's no data in the table. So the only object there will be it the row I put in my test.

First I save an Object Job having 3 fields :
Id -int-,
Type -java.lang.String-,
and Date -java.lang.Date-.

When debugging the code I see the row have been inserted correctly with a correct date (now) : Wed Apr 13 16:01:39 EDT 2005.

But when I am trying to retreive the row, by loading the "same object" I get a list with no result (no record). The problem seems to be the way I (or Hibernate) put the date in the query. If I remove the date in the "where HQL statement" I get the record I just put. But with the date parameter, MySQL cannot find the record.

Here the code I use for my test. I passe an initiated Job object with a correct date *now* and and random string for the type.

Code:
public Job storeJob(Job job){
      Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
      session.saveOrUpdate(job);
      Query q = session.createQuery("from Job as job where job.type=:type and job.date=:date");
      q.setString("type", job.getType());
      q.setDate("date", job.getDate());
      return (Job)q.list().get(0);
   }


I always get a empty list from the query, so I have an ArrayOutOfBound exception when I try to get the first (and only) record.

I have put Hibernate on Debug mode and what I see it that the date passe to the sql query is not the same:

Code:
2005-04-13 16:02:14,756 DEBUG [main] (ErrorCounter.java:72) - throwQueryException() : no errors
2005-04-13 16:02:14,756 DEBUG [main] (QueryTranslatorImpl.java:177) - HQL: from com.desjardins.vmd.etl.persistence.model.Job as job where job.type=:type and job.date=:date
2005-04-13 16:02:14,756 DEBUG [main] (QueryTranslatorImpl.java:178) - SQL: select job0_.id as id, job0_.date as date1_, job0_.type as type1_ from job job0_ where (job0_.type=? and job0_.date=?)
2005-04-13 16:02:14,756 DEBUG [main] (ErrorCounter.java:72) - throwQueryException() : no errors
2005-04-13 16:02:14,756 DEBUG [main] (AbstractFlushingEventListener.java:52) - flushing session
2005-04-13 16:02:14,756 DEBUG [main] (AbstractFlushingEventListener.java:102) - processing flush-time cascades
2005-04-13 16:02:14,772 DEBUG [main] (Cascades.java:806) - processing cascade ACTION_SAVE_UPDATE for: com.desjardins.vmd.etl.persistence.model.Job
2005-04-13 16:02:14,772 DEBUG [main] (Cascades.java:831) - done processing cascade ACTION_SAVE_UPDATE for: com.desjardins.vmd.etl.persistence.model.Job
2005-04-13 16:02:14,787 DEBUG [main] (AbstractFlushingEventListener.java:150) - dirty checking collections
2005-04-13 16:02:14,787 DEBUG [main] (AbstractFlushingEventListener.java:167) - Flushing entities and processing referenced collections
2005-04-13 16:02:14,787 DEBUG [main] (AbstractFlushingEventListener.java:203) - Processing unreferenced collections
2005-04-13 16:02:14,787 DEBUG [main] (AbstractFlushingEventListener.java:217) - Scheduling collection removes/(re)creates/updates
2005-04-13 16:02:14,787 DEBUG [main] (AbstractFlushingEventListener.java:79) - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2005-04-13 16:02:14,803 DEBUG [main] (AbstractFlushingEventListener.java:85) - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2005-04-13 16:02:14,803 DEBUG [main] (Printer.java:83) - listing entities:
2005-04-13 16:02:14,803 DEBUG [main] (Printer.java:90) - com.desjardins.vmd.etl.persistence.model.Job{date=2005-04-13 16:01:39, type=Type test, tableLoads=null, id=49}
2005-04-13 16:02:14,803 DEBUG [main] (DefaultAutoFlushEventListener.java:60) - Dont need to execute flush
2005-04-13 16:02:14,803 DEBUG [main] (AbstractBatcher.java:276) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2005-04-13 16:02:14,803 DEBUG [main] (AbstractBatcher.java:310) - select job0_.id as id, job0_.date as date1_, job0_.type as type1_ from job job0_ where (job0_.type=? and job0_.date=?)
Hibernate: select job0_.id as id, job0_.date as date1_, job0_.type as type1_ from job job0_ where (job0_.type=? and job0_.date=?)
2005-04-13 16:02:14,819 DEBUG [main] (AbstractBatcher.java:364) - preparing statement

--------------------> DATE HERE : the date is not the same, missing hour, minutes, seconds??
2005-04-13 16:02:14,819 DEBUG [main] (QueryLoader.java:232) - bindNamedParameters() Wed Apr 13 16:01:39 EDT 2005 -> date [2]
2005-04-13 16:02:14,819 DEBUG [main] (NullableType.java:59) - binding '13 April 2005' to parameter: 2

---------------------> TYPE HERE
2005-04-13 16:02:14,819 DEBUG [main] (QueryLoader.java:232) - bindNamedParameters() Type test -> type [1]
2005-04-13 16:02:14,881 DEBUG [main] (NullableType.java:59) - binding 'Type test' to parameter: 1



2005-04-13 16:02:14,881 DEBUG [main] (AbstractBatcher.java:292) - about to open ResultSet (open ResultSets: 0, globally: 0)
2005-04-13 16:02:14,881 DEBUG [main] (Loader.java:380) - processing result set
2005-04-13 16:02:14,881 DEBUG [main] (Loader.java:404) - done processing result set (0 rows)
2005-04-13 16:02:14,881 DEBUG [main] (AbstractBatcher.java:299) - about to close ResultSet (open ResultSets: 1, globally: 1)
2005-04-13 16:02:14,881 DEBUG [main] (AbstractBatcher.java:284) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2005-04-13 16:02:14,881 DEBUG [main] (AbstractBatcher.java:392) - closing statement
2005-04-13 16:02:14,881 DEBUG [main] (Loader.java:490) - total objects hydrated: 0
2005-04-13 16:02:14,881 DEBUG [main] (PersistenceContext.java:738) - initializing non-lazy collections



Any idea?

Thanks

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 4:17 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
I double check in the job.hbm.xml generated by the "Artefact Generator" and the mapping seems correct.

Code:
<property name="date" type="java.util.Date">
<column name="date" length="19" not-null="true" sql-type="datetime" />
</property>


Dont know where to search...

Thanks for answering

Etienne.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 11:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
because query.setDate() does just that; it sets the date portion of the date&time. Use query.setTimestamp()


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 7:39 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.