-->
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.  [ 6 posts ] 
Author Message
 Post subject: All you should know about Date in Hibernate
PostPosted: Tue Oct 23, 2007 2:43 pm 
Newbie

Joined: Tue Oct 23, 2007 2:36 pm
Posts: 4
Hello,

The java.util.Date and the java.sql.Date within the hibernate context is one of the most often asked question. It is without a doubt the most confusing. I am creating this thread hoping to gather all in one place the answers to this question.

Context:
a model has member variables using a java.util.Date
Date myDateOnly; //we only care about the Date not the time
Date myDateWithTime; //we want it all

the hbm.xml is defined as:
<property name="myDateOnly" column="my_date_only" type="java.util.Date" />
<property name="myDateWithTime" column="my_date_with_time" type="java.util.Date" />


the database underneath declares the type as:
my_date_only column is created as a DATE (mySQL mode)
my_date_only column is created as a DATETIME (mySQL mode)

Execution:
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-mm-dd");
Date dateOnly = dateFormatter.parse("2007/10/23");
Date dateWithTime = Calendar.getInstance().getTime();

model.setMyDateOnly(dateOnly);
model.setMyDateWithTime(dateWithTime);
model.save();

//imagine code to get a model back from db.
dateOnlyFromDb = model.getMyDateOnly();
dateWithTimeFromDb = model.getMyDateWithTime();

assertTrue(dateOnly.equals(dateOnlyFromDb));
assertTrue(dateWithTime.equals(dateWithTimeFromDb));

Now the question:
Both asserts fail. Why?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 2:45 pm 
Newbie

Joined: Tue Oct 23, 2007 2:36 pm
Posts: 4
I made a typo: imagine the format is correct everywhere (ie. yyyy-mm-dd)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 3:09 pm 
Newbie

Joined: Wed Jul 27, 2005 12:30 pm
Posts: 4
Try using type="date", type="time", or type="timestamp".

Once you correct this, your second assertion will probably still fail:
assertTrue(dateWithTime.equals(dateWithTimeFromDb));

Because you're dateWithTime was created with millisecond precision in the JVM and will probably come out of the DB with only "second" precision from the database. I'm not sure about this, but be forewarned.

(Also, a side critique, use assertEquals(dateWithTime, dateWithTimeFromDb) don't abuse assertTrue).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 5:37 pm 
Newbie

Joined: Tue Oct 23, 2007 2:36 pm
Posts: 4
junit.framework.AssertionFailedError: expected:<Tue Oct 23 14:31:28 MST 2007> but was:<2007-10-23 14:31:28.0

The date in mySQL database is fine, I am using timestamp. Why does mySQL keeps returning the milliseconds when I don't want them. This is frustating.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 7:32 pm 
Newbie

Joined: Tue Oct 23, 2007 2:36 pm
Posts: 4
I did the following changes:

the database underneath declares the type as:
my_date_only column is created as a DATE (mySQL mode)
my_date_only column is created as a TIMESTAMP (mySQL mode)

the hbm.xml is defined as:
<property name="myDateOnly" column="my_date_only" type="date" />
<property name="myDateWithTime" column="my_date_with_time" type="timestamp" />

When I compare the date from myDateWithTime I am getting:
junit.framework.AssertionFailedError: expected:<Tue Oct 23 14:31:28 MST 2007> but was:<2007-10-23 14:31:28.0

Why this extra .0 coming from the date created from the database entry?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 9:32 pm 
Newbie

Joined: Wed Jul 27, 2005 12:30 pm
Posts: 4
Try comparing their times...
assertEquals(dateWithTime.getTime(), dateWithTimeFromDb.getTime());


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