-->
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: Query update doesn't work
PostPosted: Tue Jan 15, 2013 1:59 am 
Newbie

Joined: Tue Jan 15, 2013 1:16 am
Posts: 1
I have application with spring and hibernate. I want to write JUnit test. The problem is when I execute hql query it doesn't do anything. Query executes with no exceptions. Problem affected when @Transactional annotation present. When I delete @Transactinal test passes, but I have 2 eqaul entites with different id at database. Why update does not affect? What I should I do to update entity? I also tried call flush() at dao method but it didn't solve my problem. Source code listed below.

Unit test code:
Code:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:META-INF/spring/ChequeExtractDaoTest.xml")
@Transactional
public class ChequeExtractJpaDaoTest {

    @Autowired
    @Qualifier("chequeExtractDao")
    private ChequeExtractDao chequeExtractDao;

    @Before
    public void setUp() {
       ChequeRefundsExtract extract = new ChequeRefundsExtract();
       extract.setExtractStatus(ChequeStatusExtract.STARTED);
       chequeExtractDao.save(extract);
    }
   
    @Test
    public void checkTest()
    {
      ChequeRefundsExtract extract = chequeExtractDao.getExtract();

      chequeExtractDao.makeRecordsExported();

      Assert.assertEquals(ChequeStatusExtract. EXTRACTED.toString(), extract.getExtractStatus());
    }
   
}


DAO code:
Code:
public class ChequeExtractJpaDao extends GenericJPADAO<ChequeExtract, Long> implements ChequeExtractDao {

   @Override
   public void makeRecordsExported(Date extractDate, ChequeExtract.ExtractType... type) {
      String query = "UPDATE ChequeExtract e SET e.extractStatus = :extractStatus";

      Query query = getEntityManager().createQuery(queryString);
      query.setParameter("extractStatus", ChequeStatusExtract.EXTRACTED.toString());
      query.executeUpdate();
   }

}


My Spring configuration file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      ">

   <!-- For transaction interceptors -->
   <context:annotation-config/>

   <tx:annotation-driven />

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>

   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="ChequeExtractDaoTest"/>
   </bean>

   <bean id="chequeExtractDao" class="com.integration.dao.ChequeExtractJpaDao" />

</beans>


My persistance.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0">
   <persistence-unit name="ChequeExtractDaoTest" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <class>com.integration.domain.ChequeExtract</class>
      <class>com.integration.domain.ChequeRefundsExtract</class>      
      
      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      <properties>
         <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
         <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-test"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.connection.username" value="sa"/>
         <property name="hibernate.connection.password" value=""/>
         <property name="hibernate.query.jpaql_strict_compliance" value="false" />

         <property name="hibernate.show_sql" value="true"/>
      </properties>
   </persistence-unit>
</persistence>


Top
 Profile  
 
 Post subject: Re: Query update doesn't work
PostPosted: Tue Jan 15, 2013 3:18 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Why update does not affect? What I should I do to update entity?


What you are doing is called a "bulk update".
Bulk updates are a very special feature and should generally be avoided as they act directly on the database without
synchronizing your hibernate persistent context.
I suggest you to do "normal" hiberante updates instead, that means:
1. Create or read a persistent entity object. (for example extract)
2. Change some property on that persistent entity object (for example extract.setExtractStatus(ChequeStatusExtract.STARTED);)
3. call flush(); // now the changed values on extract are propagated to the database with update-query created by hiberante automatically


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.