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>