-->
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.  [ 32 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Current Date
PostPosted: Fri Nov 07, 2003 12:59 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

Anybody knows how to get the current Date from the database using Hibernate?. I want to get the current date no matter what database I'm connected to.

Thanks


Top
 Profile  
 
 Post subject: why not java Date?
PostPosted: Fri Nov 07, 2003 2:09 pm 
Regular
Regular

Joined: Sun Sep 21, 2003 11:43 pm
Posts: 85
Location: Massachusetts
Why do you need the date from the database? Why not do a
Calendar.getInstance() or a new Date()?

Regards,
David


Top
 Profile  
 
 Post subject: Can't do a new Date ()
PostPosted: Fri Nov 07, 2003 2:12 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

In a distributed enviroment every server can have a different Date. So every new Date () get's a different value for each server in a specific point in time. So in this cases you look for one component that gives you the date, in this case the database.

Thanks


Top
 Profile  
 
 Post subject: suggestion and example
PostPosted: Fri Nov 07, 2003 3:01 pm 
Regular
Regular

Joined: Sun Sep 21, 2003 11:43 pm
Posts: 85
Location: Massachusetts
I've been reading posts about the beta Hibernate having the ability to ask that but for now, I think you're limited to manually getting the java.sql.Connection object and doing your own JDBC call on it:

Connection conn = sess.connection();
Statement stmt = conn.createStatement();
if ( stmt.execute("select CURRENT_DATE") ) {
ResultSet rs = stmt.getResultSet();
rs.next();
System.out.println("The date is: " + rs.getString(1));
}

Just DON'T do anything with that session until you've completed the JDBC call or it may wreak havok having to things going on at once in one java.sql.Connection instance.

Regards,
David

P.S. This code works with my Hibernate 2.0.3 from an open session and talking to MySQL 4.0.15 on Windows XP Home.


Top
 Profile  
 
 Post subject: Using JDBC Connection
PostPosted: Fri Nov 07, 2003 3:44 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

Yes, that is what I'm doing. The big problem is that depending on the database server the sql code changes. For example:

Oracle

select sysdate from dual;

MySQL

select now()


Top
 Profile  
 
 Post subject: Try this!
PostPosted: Sun Nov 09, 2003 1:21 am 
Newbie

Joined: Mon Oct 13, 2003 8:59 am
Posts: 3
Location: Hyderabad
Run a HQL query like this.

select new java.util.Date() from <existing className>

Will it work??

_________________
Chandu,
chandu@translogicsys.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 2:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It will work, I think .... but it will just create a new Java Date. It will not call the database's current_date function.


Top
 Profile  
 
 Post subject: How about using the Dialect OR using a time server to sync?
PostPosted: Sun Nov 09, 2003 1:15 pm 
Regular
Regular

Joined: Sun Sep 21, 2003 11:43 pm
Posts: 85
Location: Massachusetts
If you can find a way to figure out which Dialect you are using, you could make your own date class that calls the right time function over a raw JDBC connection using session.connection().

The other solution is a time server so all machines are synchronized to the second. However, you suggested you always want to ask the database the time, suggesting you have either one database server or time synchronized database servers.

If you have one database server, you can hardcode the SQL get date/time statement.

If you have multiple database servers, why don't you use the same time server or time synchronization method on the application servers so you can call a new Java date object instead of going through hoops to get the date over SQL from the database server? Both Windows and UNIX machines can use time servers to synchronize within fractions of a second.

Regards,
David


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 6:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The Oracle dialect already supports the sysdate function within the HQL select clause. I can add the MySQL now() function ASAP if you like. The all you would require is to select the correct HQL text segment in your code based on the active dialect (which can be obtained from the configuration object).

When I implemented the HQL select clause database dialect functions I had considerred aliases so we could have a single name across all dialects. Decided at the time it was extra code when it was not proven to be required. Time to reconsider it.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 7:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
When I implemented the HQL select clause database dialect functions I had considerred aliases so we could have a single name across all dialects. Decided at the time it was extra code when it was not proven to be required. Time to reconsider it.

query substitutions can help with this.


Top
 Profile  
 
 Post subject: Date solution
PostPosted: Thu Nov 13, 2003 5:54 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

Thanks for the tips

In my case I have many distributed applications accesing one database so it is important to have this functionality. The other problem is that my applications should work without modifications with Oracle, MySQL, PosgreSQL and SQL Server. One of the reasons that I'm using Object Relational Mapping tools is to get some abstraction of the database provider.

I'm looking in the configuration object and can't find the method that gets the dialect.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 6:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Look it up through the properties object. Think in terms of the hibernate properties file.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 6:04 am 
Regular
Regular

Joined: Mon Sep 08, 2003 4:53 am
Posts: 70
Location: Germany
Why not using a stored procedure (e.g. getCurrentDateTime). You could implement this specific for each database (if stored procedures are supported for the database you want to use) and call it in HQL.


Top
 Profile  
 
 Post subject: MySQL does not support Stored Procedures
PostPosted: Fri Nov 14, 2003 2:44 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi

MySQL does not support stored procedures.


Top
 Profile  
 
 Post subject: utility class perhaps...
PostPosted: Fri Nov 14, 2003 4:10 pm 
Regular
Regular

Joined: Sun Sep 21, 2003 11:43 pm
Posts: 85
Location: Massachusetts
Is there a way to get the Dialect from the sesison or session.getSessionFactory() ?

If so, you could make your own date class that, given an open session object, it could do a find() to the database with an SQL query or stored procedure request appropriate for that Dialect.

Example calling:
myclass.getDate(session);

From something like so:
public List getDate(Session session) throws HibernateException {
String dateSQLString = null;
if (Dialect == net.sf.hibernate.dialect.MySQLDialect )
dateSQLString = "select CURRENT_DATE";
else
dateSqlString = "call stored procedure that works on all other DBs";
return(session.find(dateSqlString));

You'd just need to know if/how to obtain the Dialect to make your comparison. I'd love to know how, if anyone knows, to get it from the Session or SessionFactory.

Regards,
David


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 32 posts ]  Go to page 1, 2, 3  Next

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.