Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE 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
Hibernate Eclipse IDE 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?
 
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.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic