-->
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: HQL: Compare dates
PostPosted: Fri Feb 17, 2006 6:22 am 
Newbie

Joined: Thu Feb 16, 2006 4:16 am
Posts: 17
Location: Austria
Hi,

I'm having a problem with a java.Util.Date query in HQL.

My query looks like this:
Code:
Date d = new Date(...);

[...]

Query query = em
  .createQuery("FROM duedate.DueDateDetail d "
  + " INNER JOIN FETCH d.duedate "
  + " WHERE d.dueDate = :myparam ");
  query.setParameter("myparam", d);
dueDateDetail = (Collection<DueDateDetail>) query.getResultList();



Is this query correct or how do you compare a java.util.Date with HQL? Is there anything I have to pay attention?


Best regards and thanks in advance,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 20, 2006 11:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You don't need the "INNER JOIN FETCH" line. You can compare dates in HQL just like you compare integers, so your query is fine.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 2:41 am 
Newbie

Joined: Thu Feb 16, 2006 4:16 am
Posts: 17
Location: Austria
Hello!

I need the "INNER JOIN FETCH" because the DueDate and the DueDateDetail are a master-detail joining. I know it is a little bit confusing because the d.duedate is a Set of dueDates and the dueDate is a Date.

I found out that the questioning for the exact date (date, hours, minutes and seconds) is not the best way to compare java.Util.Date ;-)

Therefore I generated two new dates to investigate my data.

Code:
Date toDate = new Date(date.getYear(), date.getMonth(), date.getDate()+1,0,0,0);
Date fromDate = new Date(date.getYear(), date.getMonth(), date.getDate(),0,0,0);

[...]

Query query = em
.createQuery("FROM duedate.DueDateDetail d "
+ " INNER JOIN FETCH d.duedate "
+ " WHERE date(d.dueDate) < :toDate "
+ " AND date(d.dueDate) > :fromDate ");


This query returns all dates from the date 'fromDate'. Additionally I used the 'date(...)' to surround my database date-field - I found the 'date(..)' in this forum but I have not tried out it without - maybe it works too.


best regards,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 5:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Don't use the Date.getYear() etc. methods. They're deprecated. If you want to comapre only bits of the date, either correctly create the appropriate Date object using Calendar methods, or use the hibernate year(), month() etc. functions.
Code:
... and year(d.dueDate) < year(:mydate)
   or (year(d.dueDate) = year(:mydate) and month(d.dueDate) < month(:mydate)
   or ...
Everything else you've got it right, though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 6:00 am 
Newbie

Joined: Sat Oct 28, 2006 6:44 am
Posts: 4
Hi there,

I tried all of the suggestions but I am unable to write a HQL statement that does work for at least MySQL(5) and Oracle9i. The problems I encountered where:

1. Function date() is usable in Oracle9i
2. Function day() is not usable in Oracle9i (although year() and month() seem to work fine)
3. Comparing dates requires an explicit cast (at least a to_timestamp for the comparing date when using a parameter)
4. MySQL doesn't know anything about to_timestamp/to_char/to_number/...

These problems lead me to the biggest problem here:

5. Hibernate does not substitute its own functions (as said so in the reference, chapter 14.9 Expressions at least for DAY()). I know that most of the functions mentioned there can be found in various databases but I thought (and the reference doesn't make it clear) that most of the functions are passed through to the SQL without substituting or checking anything.

I would like to use the Criteria API for such an approach but it doesn't support batch deletes and so I am bound to HQL. Additionally the code is invoked using the Spring Framework.

Thanks in advance for any hint or suggestion...

Regards,
C]-[aoZ


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 8:49 am 
Newbie

Joined: Sat Oct 28, 2006 6:44 am
Posts: 4
Ooops...

My fault! Sorry for this! I forgot to switch the dialect in the hibernate configuration when switching the datasource from MySQL to Oracle...

...now the correct extract() function is generated for Oracle9i...

Regards,
C]-[aoZ


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.