Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get ' current timestamp' from DB2 database in hibernate using HQL or Criteria

 
Munish Dabra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just wondering how to get the 'current timestamp or date' from DB2 database using hibernate query language(hql) or some other way in hibernate.

you can say something similar to this , but using hql or criteria or some other way in hibernate.

SELECT current timestamp FROM sysibm.sysdummy1

It is so surprising that i could not find some simple way of doing this in hibernate.

I will appreciate any help regarding that.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to do it with with SQL (see session.createSQLQuery() ). I'd suggest caution though. Using the DB server time is not going to be as portable as getting the time in the Java app and persisting that. Its usually the application's time which is important too, not the DB server's time. Which is why I think the Hibernate people have not thought it important to include in HQL.
 
Munish Dabra
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul..But sometimes one has no choice since database server and application server may be in different timezone.and making your application dependent on timezone esp when you are using timestamp to compare with one of the other columns in database is risky..Is n't it?

I tried using createSQLQuery() too...but i am getting alias exception...I will appreciate if you can tell me which persistant objec to use while writing query.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Munish Dabra:
Thanks Paul..But sometimes one has no choice since database server and application server may be in different timezone.and making your application dependent on timezone esp when you are using timestamp to compare with one of the other columns in database is risky..Is n't it?

I tried using createSQLQuery() too...but i am getting alias exception...I will appreciate if you can tell me which persistant objec to use while writing query.


I suppose you could - but it would be a fairly odd set up wouldn't it? Assuming your database(s) and app server(s) are in the same physical location, why would they be configured to use different timezones?
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use something like :
select current_timestamp() from Product pb where pb.pId='1'

The functions viz. CURRENT_DATE(), CURRENT_TIME(), CURRENT_TIMESTAMP() are native HQL functions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic