-->
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.  [ 5 posts ] 
Author Message
 Post subject: Cross-DB String representation of dates in HQL?
PostPosted: Tue Nov 04, 2008 6:12 pm 
Newbie

Joined: Tue Nov 04, 2008 5:53 pm
Posts: 3
I want to pass a query like this to Hibernate3 HQL:

Code:
from abc where dateItem > '2008-11-04'


I realise that the reference way to do this is to used a named variable in the query and then set it from Java code, but I can't easily do that because the query text is ad-hoc, built from a Velocity template.

I also realise that HQL allows scalar functions that are supported by the underlying DB (Oracle's TO_DATE, for example), but I don't want to rely on that because I don't know what my underlying DB is going to be at runtime.

So, my question is: can a query containing some kind of date literal be expressed in HQL, and it will work regardless of the actual database?

Thanks for help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 6:33 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
YYYY-MM-DD is the ANSI standard date format so it *should* work with any databases. Though, as usual, there might be exceptions. Some database, like MySQL, only supports this format. Ok, MySQL allows a lot of variants with and without delimiters, but the year, month, day order is required.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 6:46 pm 
Newbie

Joined: Tue Nov 04, 2008 5:53 pm
Posts: 3
Thanks for the reply.

Does this mean that there is no built-in way to do this kind of date comparison in the HQL, whereby Hibernate uses its knowledge of the underlying dialect to generate a correct query for that DB?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 3:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't think Hibernate does any date translations. I can't find anything in the Dialect classes that seems to be related to this. In some databases, for example PostgreSQL, the date format is configurable.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:55 pm 
Newbie

Joined: Tue Nov 04, 2008 5:53 pm
Posts: 3
Hmm, that seems odd. Why leave out dates when it does dialect-specific things for other data types? For example, I've seen it generate DB-specific versions of HQL queries that concatenate strings using ||. Not that I'm doubting your knowledge - I'd pretty much come to the same conclusion, to be honest - but it just seems odd.

If one of the Hibernate team could confirm this I'd be grateful - just want to avoid re-inventing the wheel...


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