This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

writing a HQL query with date calculation  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mySQL query below is what I want to write in HQL to obtain a list of all objects with the respective id's.
The fields in the query;
last_update_date is a date &
interval is an integer specifying the number of days.

select acct_id from profiles where sysdate() >= adddate(last_update_date, interval)

Thanks in advance for any help in figuring this out.
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out!
Didnt know i could just use mySQL functions directly in the HQL statement.
Here is the solution for anyone who might be interested;
List result = session.createQuery("from Profile as profile where sysdate() >= adddate(profile.lastUpdateDate, profile.interval ) )").list();

Originally posted by Vijay Chauhan:
The mySQL query below is what I want to write in HQL to obtain a list of all objects with the respective id's.
The fields in the query;
last_update_date is a date &
interval is an integer specifying the number of days.

select acct_id from profiles where sysdate() >= adddate(last_update_date, interval)

Thanks in advance for any help in figuring this out.


[ December 06, 2006: Message edited by: Vijay Chauhan ]
 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the cool thing about HQL, is that if Hibernate doesn't understand a particular syntax, say a call to sysdate() or your adddate function that is in SQL, then it just passes the text to the database. Meaning if the database suports those functions it will work. So it means that your HQL query doesn't have to always have just HQL, but some SQL can work, but remember when referring to Objects and Attributes you need those object.attribute referencing.

Mark
 
Vijay Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that explanation Mark! And yes that is really cool about hibernate that instead of breaking, it just attempts to send it to the DB as text.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!