-->
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.  [ 14 posts ] 
Author Message
 Post subject: Hibernate's ability to query time
PostPosted: Mon Mar 08, 2004 9:19 am 
Beginner
Beginner

Joined: Tue Feb 10, 2004 5:48 am
Posts: 20
I have the following problem:
I have a row in my Oracle 9 database with a field "Time" of type DATE. In this field is the following: 2000-01-01 01:34:56
When I do a Hibernate comparison (Expression.ge etc...) it doesn't do the comparison on the time properly, even though its mapped through java.sql.Time to a custom Time type (that extends java.util.Date). I am also using Hibernate.TIME in my conversion class, TimeType. It seems to use a default date value. Indeed, in the output, I get:
binding '01:34:56' to parameter: 1
but I expected to get
binding '1 January 2000 01:34:56' to parameter:1

QUESTION: Is there a known issue with Hibernate when querying times in database DATE fields? If so, do I need to build the query manually for times with net.sf.hibernate.expression.sql(query)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 9:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
If you use TIME you get the time portion, if you use date you get the DATE portion or you use TIMESTAMP you get both.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Mar 08, 2004 9:59 am 
Beginner
Beginner

Joined: Tue Feb 10, 2004 5:48 am
Posts: 20
Well, the whole problem was that just using Hibernate.TIME, the query didn't query the whole DATE in the database, as you suggested. Which is a bit odd, because Oracle doesn't have a dedicated TIME type (that Hibernate.TIME would logically reflect). I used Hibernate.TIMESTAMP to correspond to the database DATE, and the query is now over the whole DATE, which is great ;-) Thanks.


Top
 Profile  
 
 Post subject: Hibernate.TIME problem
PostPosted: Tue Mar 09, 2004 3:17 am 
Beginner
Beginner

Joined: Tue Feb 10, 2004 5:48 am
Posts: 20
For legacy reasons, we can't use Hibernate.TIMESTAMP. When using Hibernate.TIME, it tries to select only the time portion from the Oracle database, but doesn't fill in the date part correctly, I think its using Oracle's default date or an internal default date somehow. This seems to be a problem with Hibernate, rather than the database, as it is not doing what it says its doing...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 3:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the Javadoc of java.sql.Time and java.sql.Timestamp before saying silly things. TIA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 3:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
My apologies, that was too harsh. Let me explain:

You can't use Hibernate unless you are familiar with JDBC first. Hibernate builds on top of JDBC and so a great deal of functionality, such as date handling, is delegated directly to JDBC and works exactly like the underlying JDBC.

We think that all Java developers should know how dates work in JDBC!


Top
 Profile  
 
 Post subject: Hmm
PostPosted: Tue Mar 09, 2004 3:45 am 
Beginner
Beginner

Joined: Tue Feb 10, 2004 5:48 am
Posts: 20
Thank you for your response and apology. That was indeed harsh, and goes completely against the point of a forum.

I have read java.sql.Time and Timestamp and have experience with JDBC. Yet I don't see at all how they apply to my question. Perhaps my question is not clear to you. Let me rephrase it:

I am trying to use Hibernate to query over the time parts of a field X in the Oracle database. Field X is of Oracle type DATE. When I do a query, the time I specify binds to the necessary parameter in the Hibernate query. However, this doesn't seem to ignore the date portions of the X fields. For example, say the following rows exist:

(1, '2000-01-01 01:12:00')
(2, '2000-01-01 01:13:00')
(3, '2000-01-01 01:14:00')

My input to the Hibernate query (using Hibernate.TIME in the conversion class) is to find all rows with times greater than '01:13:00' with format 'HH:mm:ss'. For some reason, Hibernate returns all 3 rows. Why?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 11:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
....how would you express that in JDBC ? (just curious coz' i always get different behavior regarding this issue when talking to different db's ...even when using standard JDBC ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: ...
PostPosted: Tue Mar 09, 2004 11:20 am 
Beginner
Beginner

Joined: Tue Feb 10, 2004 5:48 am
Posts: 20
Well I would use a java.sql.Time object (instiating it with the help of a DateFormat no doubt), and query based on it. I have noticed that when I create a java.sql.Time involving the Hibernate.TIME type, and insert it into the database (into a column of type DATE), Hibernate fills in the date portion to be '1900-01-01'. Very odd I think, especially when using the Hibernate.TIMESTAMP type and specifying just a time portion, Hibernate fills the date portion in to be 1970-01-01, the standard Java first epoch. Why the difference? Hibernate.TIME doesn't seem to conform to java.sql.Time which uses the first Java epoch...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 11:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please take a look at net.sf.hibernate.type.TimeType before posting any false assumptions.


Top
 Profile  
 
 Post subject: Re: ...
PostPosted: Tue Mar 09, 2004 11:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
virgo_ct wrote:
Well I would use a java.sql.Time object (instiating it with the help of a DateFormat no doubt), and query based on it. I have noticed that when I create a java.sql.Time involving the Hibernate.TIME type, and insert it into the database (into a column of type DATE), Hibernate fills in the date portion to be '1900-01-01'. Very odd I think, especially when using the Hibernate.TIMESTAMP type and specifying just a time portion, Hibernate fills the date portion in to be 1970-01-01, the standard Java first epoch. Why the difference? Hibernate.TIME doesn't seem to conform to java.sql.Time which uses the first Java epoch...


eh - your assumptions don't make sense to me ;)

This behavior is how TIME, DATE and TIMESTAMP behaves - also in JDBC...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 4:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are, of course, quite wrong. Hibernate behavior is identical to JDBC. It is easy to verify this by testing, or looking at the sourcecode.


Top
 Profile  
 
 Post subject: Finally
PostPosted: Wed Mar 10, 2004 4:44 am 
Beginner
Beginner

Joined: Tue Feb 10, 2004 5:48 am
Posts: 20
The direct and clear answer I was looking for on this forum, but had to figure out from your often harsh responses was that the JDBC driver for Oracle uses a default date of '1900-01-01' when either JDBC or Hibernate queries are done, which DOES NOT agree with java.sql.Time, which assumes a default date of '1979-01-01'.

Thus, my concerns about Hibernate acting incorrectly were wrong, and I apologise, BUT the fact that Hibernate is affected by a strange (in my opinion) JDBC driver issue is the reason I had to ask the questions, as it seemed that the problems were coming form Hibernate. I'm sorry I troubled you with this issue, obviously this forum is only for experts on JDBC and Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 1:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
There was one harsh answer which were apologized right after....not a biggie, right ?

Second, the solution to the problem exactly shows that users (and that also includes me) should try to investigate the bugs by e.g. tracing down via a debugger and/or look at the sql executed and try to do it via a database tool or similar to show wether it is a hibernate thing or simply just how stuff works ..... we don't wanna be harsh when answering questions, but we also expect users to do a tad more investigation before asking ;)

peace out ;)

_________________
Max
Don't forget to rate


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