-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting datbase time
PostPosted: Mon Jul 10, 2006 4:12 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
Hi.

Which is the fastest way to get the database time?
How to execute queries like "select getdate()" or "select systime from dual" in hibernate?

Tanks
markov


Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 8:45 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
I created my own "dual" table in hsql similar to Oracle's built in table.

Then I created this function (which uses Spring's HibernateDaoSupport, but you could get the session in another way):

Code:
    public Date getDate() {
        Session session = getHibernateTemplate().getSessionFactory().openSession();
        SQLQuery query = session.createSQLQuery("select curdate() as mydate from dual");
        query.addScalar("mydate", Hibernate.DATE);
        return (Date) query.uniqueResult();
    }


This worked for me,


Top
 Profile  
 
 Post subject: Re: Getting datbase time
PostPosted: Mon Jul 10, 2006 2:00 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
markov wrote:
Which is the fastest way to get the database time?
How to execute queries like "select getdate()" or "select systime from dual" in hibernate?


If you want to execute the SQL directly without trying some workaround in HQL, this is how to do it:

(Of course, you could abstract this into two functions, one private that accepts a sql string parameter and one public that calls the private. This would allow you to have multiple functions without rewriting the command and reader objects.)

Code:
public static DateTime CurrentDateTime()
{
  System.Data.IDbCommand cmd =
    _yourSessionObject.Connection.CreateCommand();
  cmd.CommandText = "select sysdate from dual";
  System.Data.IDataReader rdr = cmd.ExecuteReader();

  if (rdr.Read())
    return (DateTime)rdr[0];
  else
    throw new InvalidOperationExeception();
}


Top
 Profile  
 
 Post subject: --
PostPosted: Fri Jul 28, 2006 3:25 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
I can't beleve that such a simple things cannot be realised in hibernate..
So how to map select getdate()??? Is there no simple way to get the time from different database types?

cheers
markov


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