-->
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.  [ 8 posts ] 
Author Message
 Post subject: Can't get a java.util.Date back
PostPosted: Sat Sep 25, 2004 5:22 am 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
Using hibernate 2.1 attached to PostgreSQL 7.4.5, I'm havig trouble getting the current time back from the database using hibernate.

My code:

public static java.util.Date getDbCurrentTime() throws
HibernateException
{
Query query = session.createSQLQuery("select CURRENT_TIMESTAMP", "date", java.util.Date.class);
java.util.Date dbTime = (java.util.Date)query.uniqueResult();
log.debug("getDbCurrentTime(): " + dbTime);
return dbTime;
}

When I run this, it fails with an exception
net.sf.hibernate.MappingException: No persister for: java.util.Date
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:347)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2690)
at net.sf.hibernate.impl.SessionImpl.getSQLLoadable(SessionImpl.java:3817)
at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3796)
at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)
at net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:550)
at com.whatever.DataStore.getDbCurrentTime(DataStore.java:60)

I'm not sure how I would set up a mapper for this since it's not grabbing anything from a table. And besides, doesn't hibernate already know how to map a Date object?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 6:39 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
In most cases you won't get a java.util.Date but a java.sql.Date instances (as per the JDBC specs)...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 8:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
createSQL query can only return Hibernate mapped classes.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 25, 2004 11:30 pm 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
So then what can I do to get the current date back from the database using hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 26, 2004 5:29 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
mock1010 wrote:
So then what can I do to get the current date back from the database using hibernate?


No, Michael said SQL queries... not HQL queries ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 26, 2004 9:19 am 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
Unfortunately, it doesn't work with HQL either. Running a query created with session.createQuery("select CURRENT_TIMESTAMP"); causes hibernate to generate and run the SQL query "select CURRENT_TIMESTAMP as x0_0_ from", which naturally doesn't work.

I don't really care about what mechanism I use to do it; I just want to get the current time from the database. Does anyone know how to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 26, 2004 9:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
session.connection().prepareStatement("select current_timestamp").executeQuery();


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 26, 2004 9:57 am 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
Ah thanks. that did the trick.


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