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.