Wednesday, August 18, 2010

Two ways to select the current date from the database in Hibernate And Joda Time

The first method - this requires that a table is mapped to a hibernatified object named "Company" and is always available. This won't work if you are dealing with several schemas unless all of them share at least a single table name:
DateTimeZone databaseTimeZone = DateTimeZone.forID(getCurrentSession().createSQLQuery("select DBTIMEZONE from dual").uniqueResult().toString()); return new DateTime(getCurrentSession().createQuery("select current_timestamp() from Company").list().get(0), databaseTimeZone);
Another way is specific to the database platform you are using. This uses the standard Oracle method of "select SYSDATE from dual" to return back a timestamp which contains all of date, time, and timezone.
Timestamp timestamp = (Timestamp) getCurrentSession().createSQLQuery("select SYSDATE param from dual").addScalar("param", Hibernate.TIMESTAMP).uniqueResult(); return new DateTime(timestamp);

No comments:

Post a Comment

AddThis

Bookmark and Share