-->
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.  [ 2 posts ] 
Author Message
 Post subject: Use Example/Criteria to build query w/ Date/time comparison
PostPosted: Thu Sep 08, 2005 10:41 am 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
I have a class called User, which has a few properties. One of the properties
is last time a user logged into the application. I am writing some code for
searching for users who logged into the application before a certain time point.

I use Hibernate's Example and Criteria to build the query code as follows:

Example exampleUser = Example.create(user).ignoreCase().enableLike(MatchMode.ANYWHERE);
SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy HH:mm:ss");
Criteria crit = this.getSession().createCriteria(User.class).add(exampleUser);
crit.add(Expression.le("lastLogin", "'12/11/2005 11:11:11'"));
return crit.list();

When the code runs to "return crit.list()", there is the following exception thrown:

10:34:37,437 WARN AbstractBatcher:208 - exception clearing maxRows/queryTimeout
java.sql.SQLException: Invalid state, the PreparedStatement object is closed.
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.checkOpen(JtdsPreparedStatement.java:181)
at net.sourceforge.jtds.jdbc.JtdsStatement.getMaxRows(JtdsStatement.java:671)
at org.hibernate.jdbc.AbstractBatcher.closeQueryStatement(AbstractBatcher.java:204)
at org.hibernate.jdbc.AbstractBatcher.closeStatements(AbstractBatcher.java:254)
at org.hibernate.jdbc.ConnectionManager.cleanup(ConnectionManager.java:259)
at org.hibernate.jdbc.ConnectionManager.close(ConnectionManager.java:194)
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:289)
at org.springframework.orm.hibernate3.SessionFactoryUtils.doClose(SessionFactoryUtils.java:751)
at org.springframework.orm.hibernate3.SessionFactoryUtils.closeSessionOrRegisterDeferredClose(SessionFactoryUtils.java:740)
at org.springframework.orm.hibernate3.SessionFactoryUtils.releaseSession(SessionFactoryUtils.java:720)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCleanupAfterCompletion(HibernateTransactionManager.java:568)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:607)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:487)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCloseTransactionAfterThrowing(TransactionAspectSupport.java:278)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy17.findUsers(Unknown Source)
....


Did some research but got no clue....Is the above code right? Any help/info is greatly appreciated!

I am using Hibernate 3.0.3 and MS SQL server.

Thanks,
Pete


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 1:44 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
For the record, I found the answer. I should not have done:

crit.add(Expression.le("lastLogin", "'12/11/2005 11:11:11'"));

instead, it needs to something such as:

crit.add(Expression.le("lastLogin", new Date()));


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