-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to use a Joda PersistentDateTime in a HQL where clause ?
PostPosted: Tue Nov 23, 2010 8:41 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello,

I have a domain class with a property having a org.joda.time.contrib.hibernate.PersistentDateTime mapping and I'm trying to run an HQL query with a Joda date on it.

Here is the mapping:

Code:
      <property name="contactDateTime"
         type="org.joda.time.contrib.hibernate.PersistentDateTime">
         <column name="contact_datetime" />
      </property>


Here is the domain class:

Code:
   private DateTime contactDateTime;

   public DateTime getContactDateTime() {
      return this.contactDateTime;
   }

   public void setContactDateTime(DateTime contactDateTime) {
      this.contactDateTime = contactDateTime;
   }



Here is the Dao and its HQL statement:

Code:
          Query query = getSession().createQuery("delete from Contact where contactDateTime is not null and contactDateTime >= ?");
          query.setDate(0, sinceDate.toDate());
          int count = query.executeUpdate();


Here is the test case:

Code:
   public ContactDaoTest() {
      contact0 = new Contact();
      contact0.setContactDateTime(new DateTime());
      contact0.setMessage("Hello Marc");
      contact1 = new Contact();
      contact1.setContactDateTime(new DateTime().plusDays(1));
      contact1.setMessage("Hello Steph");
      contact2 = new Contact();
      contact2.setContactDateTime(new DateTime().plusDays(2));
      contact2.setMessage("Hello Cyr");
   }

   public void testDeleteByDate() {
      contact0 = contactDao.makePersistent(contact0);
      contact1 = contactDao.makePersistent(contact1);
      contact2 = contactDao.makePersistent(contact2);
      DateTime sinceDate = new DateTime().plusDays(1);
      logger.debug("The date sinceDate : " + sinceDate);
      logger.debug("The date of contact0 : " + contact0.getContactDateTime());
      logger.debug("The date of contact1 : " + contact1.getContactDateTime());
      logger.debug("The date of contact2 : " + contact2.getContactDateTime());
      long countDeleted = contactDao.deleteByDate(sinceDate);
      assertEquals(2, countDeleted);
      List<Contact> contacts = contactDao.findAll();
      assertEquals(1, contacts.size());
   }


And finally, the exception:

Quote:
2010-11-23 13:22:50,411 DEBUG [ContactDaoTest] The date sinceDate : 2010-11-24T13:22:50.411+01:00
2010-11-23 13:22:50,411 DEBUG [ContactDaoTest] The date of contact0 : 2010-11-23T13:22:50.382+01:00
2010-11-23 13:22:50,411 DEBUG [ContactDaoTest] The date of contact1 : 2010-11-24T13:22:50.382+01:00
2010-11-23 13:22:50,411 DEBUG [ContactDaoTest] The date of contact2 : 2010-11-25T13:22:50.382+01:00
2010-11-23 13:22:50,411 DEBUG [AnnotationTransactionAttributeSource] Adding transactional method [deleteByDate] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2010-11-23 13:22:50,411 DEBUG [TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1bf8cd5] for key [org.hibernate.impl.SessionFactoryImpl@d28dfa] bound to thread [main]
2010-11-23 13:22:50,411 DEBUG [HibernateTransactionManager] Found thread-bound Session [org.hibernate.impl.SessionImpl@1a631c2] for Hibernate transaction
2010-11-23 13:22:50,411 DEBUG [TransactionSynchronizationManager] Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@1384ed5] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@8be9ef] bound to thread [main]
2010-11-23 13:22:50,411 DEBUG [HibernateTransactionManager] Participating in existing transaction
2010-11-23 13:22:50,411 DEBUG [TransactionInterceptor] Getting transaction for [com.thalasoft.learnintouch.core.dao.ContactDao.deleteByDate]
2010-11-23 13:22:50,411 DEBUG [TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1bf8cd5] for key [org.hibernate.impl.SessionFactoryImpl@d28dfa] bound to thread [main]
2010-11-23 13:22:50,411 DEBUG [TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1bf8cd5] for key [org.hibernate.impl.SessionFactoryImpl@d28dfa] bound to thread [main]
2010-11-23 13:22:50,654 DEBUG [SQL]
delete
from
contact
where
(
contact_datetime is not null
)
and contact_datetime>=?
Hibernate:
delete
from
contact
where
(
contact_datetime is not null
)
and contact_datetime>=?
2010-11-23 13:22:50,655 DEBUG [DateType] binding '24 November 2010' to parameter: 1
2010-11-23 13:22:50,658 DEBUG [TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1bf8cd5] for key [org.hibernate.impl.SessionFactoryImpl@d28dfa] bound to thread [main]
2010-11-23 13:22:50,658 DEBUG [TransactionInterceptor] Completing transaction for [com.thalasoft.learnintouch.core.dao.ContactDao.deleteByDate]
2010-11-23 13:22:50,659 DEBUG [SpringMethodRoadie] Test method [public void com.thalasoft.learnintouch.core.dao.ContactDaoTest.testDeleteByDate()] threw exception: null
2010-11-23 13:22:50,661 DEBUG [TestContextManager] afterTestMethod(): instance [com.thalasoft.learnintouch.core.dao.ContactDaoTest@14562c5], method [public void com.thalasoft.learnintouch.core.dao.ContactDaoTest.testDeleteByDate()], exception [null]
2010-11-23 13:22:50,661 DEBUG [TransactionalTestExecutionListener] Retrieved @TransactionConfiguration [null] for test class [class com.thalasoft.learnintouch.core.dao.ContactDaoTest]
2010-11-23 13:22:50,661 DEBUG [TransactionalTestExecutionListener] Retrieved TransactionConfigurationAttributes [[TransactionConfigurationAttributes@92eb86 transactionManagerName = 'transactionManager', defaultRollback = true]] for class [class com.thalasoft.learnintouch.core.dao.ContactDaoTest]
2010-11-23 13:22:50,661 DEBUG [TransactionalTestExecutionListener] No method-level @Rollback override: using default rollback [true] for test context [[TestContext@fec107 testClass = ContactDaoTest, locations = array<String>['classpath:spring-hibernate.xml', 'classpath:spring-hibernate-dao.xml', 'classpath:spring-data-source.xml'], testInstance = com.thalasoft.learnintouch.core.dao.ContactDaoTest@14562c5, testMethod = testDeleteByDate@ContactDaoTest, testException = [null]]]
2010-11-23 13:22:50,661 DEBUG [TransactionalTestExecutionListener] Ending transaction for test context [[TestContext@fec107 testClass = ContactDaoTest, locations = array<String>['classpath:spring-hibernate.xml', 'classpath:spring-hibernate-dao.xml', 'classpath:spring-data-source.xml'], testInstance = com.thalasoft.learnintouch.core.dao.ContactDaoTest@14562c5, testMethod = testDeleteByDate@ContactDaoTest, testException = [null]]]; transaction manager [org.springframework.transaction.support.DefaultTransactionStatus@1a4ea4f]; rollback [true]
2010-11-23 13:22:50,661 DEBUG [HibernateTransactionManager] Triggering beforeCompletion synchronization
2010-11-23 13:22:50,661 DEBUG [HibernateTransactionManager] Initiating transaction rollback
2010-11-23 13:22:50,661 DEBUG [HibernateTransactionManager] Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1a631c2]
2010-11-23 13:22:50,662 DEBUG [HibernateTransactionManager] Triggering afterCompletion synchronization
2010-11-23 13:22:50,662 DEBUG [TransactionSynchronizationManager] Clearing transaction synchronization
2010-11-23 13:22:50,662 DEBUG [TransactionSynchronizationManager] Removed value [org.springframework.orm.hibernate3.SessionHolder@1bf8cd5] for key [org.hibernate.impl.SessionFactoryImpl@d28dfa] from thread [main]
2010-11-23 13:22:50,662 DEBUG [TransactionSynchronizationManager] Removed value [org.springframework.jdbc.datasource.ConnectionHolder@1384ed5] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@8be9ef] from thread [main]
2010-11-23 13:22:50,662 DEBUG [HibernateTransactionManager] Closing Hibernate Session [org.hibernate.impl.SessionImpl@1a631c2] after transaction
2010-11-23 13:22:50,662 DEBUG [SessionFactoryUtils] Closing Hibernate Session
2010-11-23 13:22:50,663 DEBUG [SessionFactoryUtils] Could not close Hibernate Session
org.hibernate.SessionException: Session was already closed
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:276)
at org.springframework.orm.hibernate3.SessionFactoryUtils.closeSession(SessionFactoryUtils.java:791)
at org.springframework.orm.hibernate3.SessionFactoryUtils.closeSessionOrRegisterDeferredClose(SessionFactoryUtils.java:777)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCleanupAfterCompletion(HibernateTransactionManager.java:733)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:989)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:855)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:800)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:501)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:277)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:170)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:344)
at org.springframework.test.context.junit4.SpringMethodRoadie.runAfters(SpringMethodRoadie.java:307)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:338)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
2010-11-23 13:22:50,674 WARN [TestContextManager] Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@1893efe] to process 'after' execution for test: method [public void com.thalasoft.learnintouch.core.dao.ContactDaoTest.testDeleteByDate()], instance [com.thalasoft.learnintouch.core.dao.ContactDaoTest@14562c5], exception [null]
org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:677)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:823)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:800)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:501)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.endTransaction(TransactionalTestExecutionListener.java:277)
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.afterTestMethod(TransactionalTestExecutionListener.java:170)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:344)
at org.springframework.test.context.junit4.SpringMethodRoadie.runAfters(SpringMethodRoadie.java:307)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:338)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:674)
... 28 more
2010-11-23 13:22:50,676 DEBUG [DirtiesContextTestExecutionListener] After test method: context [[TestContext@fec107 testClass = ContactDaoTest, locations = array<String>['classpath:spring-hibernate.xml', 'classpath:spring-hibernate-dao.xml', 'classpath:spring-data-source.xml'], testInstance = com.thalasoft.learnintouch.core.dao.ContactDaoTest@14562c5, testMethod = testDeleteByDate@ContactDaoTest, testException = [null]]], dirtiesContext [false].
Tests run: 8, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.15 sec <<< FAILURE!

Results :

Tests in error:
testDeleteByDate(com.thalasoft.learnintouch.core.dao.ContactDaoTest)

Tests run: 8, Failures: 0, Errors: 1, Skipped: 0

2010-11-23 13:22:50,725 INFO [GenericApplicationContext] Closing org.springframework.context.support.GenericApplicationContext@1adc30: display name [org.springframework.context.support.GenericApplicationContext@1adc30]; startup date [Tue Nov 23 13:22:44 CET 2010]; root of context hierarchy
2010-11-23 13:22:50,743 DEBUG [GenericApplicationContext] Publishing event in context [org.springframework.context.support.GenericApplicationContext@1adc30]: org.springframework.context.event.ContextClosedEvent[source=org.springframework.context.support.GenericApplicationContext@1adc30: display name [org.springframework.context.support.GenericApplicationContext@1adc30]; startup date [Tue Nov 23 13:22:44 CET 2010]; root of context hierarchy]
2010-11-23 13:22:50,743 INFO [DefaultListableBeanFactory] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@949f69: defining beans [sessionFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,hibernateTemplate,org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,addressDao,adminDao,adminModuleDao,adminOptionDao,clientDao,contactDao,contactStatusDao,contactRefererDao,containerDao,contentImportDao,contentImportHistoryDao,documentCategoryDao,documentDao,flashDao,formDao,formItemDao,formItemValueDao,formValidDao,guestbookDao,linkCategoryDao,linkDao,mailCategoryDao,mailDao,userDao,preferenceDao,webpageDao,webpageDirectoryDao,dataSource,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
2010-11-23 13:22:50,744 DEBUG [DefaultListableBeanFactory] Retrieved dependent beans for bean 'contactRefererDao': [com.thalasoft.learnintouch.core.dao.ContactDaoTest]
2010-11-23 13:22:50,744 DEBUG [DisposableBeanAdapter] Invoking destroy() on bean with name 'sessionFactory'
2010-11-23 13:22:50,744 INFO [LocalSessionFactoryBean] Closing Hibernate SessionFactory
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.


Any clue ?


Top
 Profile  
 
 Post subject: Re: How to use a Joda PersistentDateTime in a HQL where clause ?
PostPosted: Wed Nov 24, 2010 10:45 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
So many views and not a single post ?


Top
 Profile  
 
 Post subject: Re: How to use a Joda PersistentDateTime in a HQL where clause ?
PostPosted: Thu Nov 25, 2010 3:17 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
It now works fine. And I don't know why.


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