Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 40 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Timestamp mapped to Date ?
PostPosted: Tue Nov 04, 2003 7:27 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Hi,

I'm using the codegenerator and it maps 'timestamp' in the mapping file to 'java.util.Date'

Shouldn't it generate java.sql.Timestamp???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 8:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummm. java.sql.Timestamp extends java.util.Date


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 8:19 am 
Senior
Senior

Joined: Tue Oct 21, 2003 8:15 am
Posts: 186
Soooooo?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 1:14 pm 
Newbie

Joined: Mon Dec 22, 2003 10:00 am
Posts: 11
Location: Switzerland
I have the same problem...
java.sql.Timestamp extends java.util.Date by adding a nanoseconds field, thus they are not the same thing.
Is there a solution to this code-generation problem?
Thank you!
Regards
Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2004 6:16 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Have a look at http://forum.hibernate.org/viewtopic.php?t=927602

This may give you some hints ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 5:13 am 
Newbie

Joined: Mon Dec 22, 2003 10:00 am
Posts: 11
Location: Switzerland
Thank you brenuart, but I still miss the point.
Why does TimestampType.getReturnedClass() return a java.util.Date instead of a java.sql.Timestamp?
Why does hibernate in this case choose the right type for me? :)
Thank you
Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 6:35 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
mapofe wrote:
Thank you brenuart, but I still miss the point.
Why does TimestampType.getReturnedClass() return a java.util.Date instead of a java.sql.Timestamp?


Ask Gavin ;-)

To be correct, Hibernate DOES return a java.sql.Timestamp but casted to a java.util.Date...

Although java.sql.Timestamp is assign-compatible with a java.util.Date - it is not exactly the same, in the sense that Timestamp.equals(Date) will not work... Quite annoying :-(

Here is what you should do - beside asking to change Hibernate ;-) - is to create a UserType for what you need:
- either your own TimestampUserType that would return a Timestamp - if this is what you need;
- either your own DateTimeUserType that would convert the java.sql.Timestamp into a concrete java.util.Date (if you need Date+Time precision returned as a java.util.Date).


You are not the first having this problem - although the Jira issue mentionned in the URL shown above is closed - maybe you could still vote for it ;-)

I had to write such DateTimeUserType for our project - I can post it here if needed.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 6:38 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Note that in your case - if you need a java.sql.Timestamp - you shouldn't have anything to modify.

Just declare your properties as java.sql.Timestamp, map them as an Hibernate Timestamp - and everything should be ok.

May be it will fail since TimestampType.getReturnedClass() is a java.util.Date - no idea if it will work...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 4:03 am 
Newbie

Joined: Tue Oct 14, 2003 11:32 am
Posts: 9
Location: Switzerland
Hi brenuart, thank you for your answer.
I'll use a subclass of TimestampUserType, with the big uncertainty "will it work or is it broken? Why does Hibernate map property type="timestamp" to a java.sql.Date"?.
Thank you!
Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 4:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Why would you want to pollute your domain model with JDBC-specific classes from the package java.sql????

Your domain model should not have dependencies to persistence infrastructure, if possible.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 5:24 am 
Newbie

Joined: Tue Oct 14, 2003 11:32 am
Posts: 9
Location: Switzerland
Hi Gavin,
this application binds to a legacy database, and the application must handle dates with nanoseconds (timestamp) precision, i.e. the database contains history information based on timestamps, so I have to (or at least that's what I think) map these persistent fields to java.sql.Timestamps.
Anyway IMHO if you provide a TimestampType than it should work with Timestamps at 100% for consistency.
After all Hibernate is an SQL based persistence layer, thus Timestamp is an acceptable java type.
Regards
Massimo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Dude, Timestamp inherits Date! Think about it. Hibernate has no problem assigning an instance of Timestamp to a property of type Date.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:35 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
gavin wrote:
Dude, Timestamp inherits Date! Think about it. Hibernate has no problem assigning an instance of Timestamp to a property of type Date.


(will work, except for the Timestamp.equals(Date), Gavin, except for the equals()... :-(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
huh??? What, Java has no polymorphism?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:47 pm 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
maybe try Date.equals(Timestamp) if your doing a compare against a regular Date the nanos won't be there anyway.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 40 posts ]  Go to page 1, 2, 3  Next

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.