-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to get current date?
PostPosted: Thu Mar 17, 2005 1:27 pm 
Newbie

Joined: Thu Mar 17, 2005 1:03 pm
Posts: 4
Hi, I'm embarrassed to have to ask this, but can someone tell me how to query for the current date?

I'm able to do this with a Hibernate Session using just straight JDBC code, but it's my understanding that not all databases have a "dual" table. So, my query of "select sysdate from dual" may not be portable.

I've searched Google for hours, and the FAQs, but I must be using bad search strings because I'm not finding this! I see lots of Hibernate code out there (including in the Hibernate documentation) that compares dates against "current date", so I'm thinking there's a way to get "current date" in a nice portable way using Hibernate.

Can anyone help me rewrite this in a happy, portable way? Thank you!

Hibernate version: 2.1.8

Code between sessionFactory.openSession() and session.close():
Code:
Session session = HibernateContext.getSession("java:/hibernate/SessionFactory");
                Connection connection = session.connection();
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery("select sysdate from dual");
                rs.next();
                this.timestamp = rs.getTimestamp(FIRST_COLUMN);
                rs.close();
                session.close();


Name and version of the database you are using:Oracle 9i 9.2.0.3


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 3:56 pm 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
Why query for it? Why not create a java.util.Date object to get the current date.
Code:
new Date();

creates a current date.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 6:25 pm 
Newbie

Joined: Thu Mar 17, 2005 6:17 pm
Posts: 1
I want to know the date at the database level, not the jvm level. Doing new Date() gives me the date at the jvm level, which in my case is a different machine than where the database is.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 12:09 am 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
current_date is a SQL-92 function, which should give you a standard if not portable way of getting the date through the database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 1:57 pm 
Newbie

Joined: Thu Mar 17, 2005 8:44 pm
Posts: 6
Actually hibernate doesn't support calling database-specific SQL functions from the select clause for all Hibernate SQL Dialects. Although same sql functions might work in the where clause.
But i think it does support that for Oracle...

try something like this from your HQL :

select sysdate from DataBean db

Here DataBean is your persistent class and db is alias.

I hope that will work if you are lucky enough. :). I don't know why gavin or hibernate team didn't mention in the hibernate documentation that which sql functions from which database are supported in the select clause.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 2:27 pm 
Newbie

Joined: Sat Sep 13, 2003 12:45 am
Posts: 12
Location: Rochester, NY
If all that's keeping you from doing what you want is the absence of a table called dual why don't you just plan on creating one in any database that doesn't have one? Or if you don't like using dual just have another application table called MY_TABLE_THAT_I_USE_TO_GET_THE_DATE_FROM. (you get the idea)

_________________
Jim


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 2:49 pm 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
What a let-down!

In addition to creating a dual table where they do not exist, you could
also make a view that has the same name in all your dbs.
In ms sql server:
CREATE VIEW my_time
AS
SELECT CURRENT_TIMESTAMP AS my_date

You can even map it in hibernate...


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