-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query Database Time with Hibernate?
PostPosted: Sun Dec 11, 2005 6:00 am 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Is there a way using Hibernate to query the database for the database server's current time? This is particularly useful when needing to synchronize data on multiple client machines each running a Hibernate application with a centralized time residing on the database server.

Hibernate version: 3.0

Mapping documents: N/A

Code between sessionFactory.openSession() and session.close(): N/A

Full stack trace of any exception that occurs: N/A

Name and version of the database you are using: N/A

The generated SQL (show_sql=true): N/A

Debug level Hibernate log excerpt: N/A


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 11, 2005 10:57 am 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
In general, using time/date to synchronize is troublesome and error prone. With that disclaimer :

If you only want to work with a single server db (or a couple) then you might try :

DatabaseMetaData meta = connection.getMetaData();
// Get the list of time and date functions
String timedateFunctions[] = meta.getTimeDateFunctions().split(",\\s*");

This will give you a list of the db specific functions provided by the database and you might find one that's useful. You could also write a stored proc for the database that updates last-sync-time for a client at the end of your data generation. Then use that to select new data on next sync.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 3:56 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
Another option would to create a view in the database that returns the current time. You could then query the view using Hibernate as if it were a table. For example, in Oracle the view could be one of:

CREATE VIEW CURRENT_TIME AS SELECT SYSDATE CURRENT_TIME FROM DUAL;

CREATE VIEW CURRENT_TIMESTAMP AS SELECT SYSTIMESTAMP CURRENT_TIMESTAMP FROM DUAL;

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 4:10 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
HQL has the following functions:

current_date(), current_time(), current_timestamp()

See http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql-expressions

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 5:53 pm 
Newbie

Joined: Mon Nov 21, 2005 1:48 pm
Posts: 17
Assuming that I have acquired the db specific function name, how do I go about executing it from within Hibernate?

FMcGeough wrote:
In general, using time/date to synchronize is troublesome and error prone. With that disclaimer :

If you only want to work with a single server db (or a couple) then you might try :

DatabaseMetaData meta = connection.getMetaData();
// Get the list of time and date functions
String timedateFunctions[] = meta.getTimeDateFunctions().split(",\\s*");

This will give you a list of the db specific functions provided by the database and you might find one that's useful. You could also write a stored proc for the database that updates last-sync-time for a client at the end of your data generation. Then use that to select new data on next sync.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.