I have recenty started researching the use of Spring Data Repositories and created a small test application. Besides the standard definition of a domain object, a repository interface extended from CRUDRepository I have the following context file:
and a small JUnit test: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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schem...ng-jpa-1.1.xsd "> <jpa:repositories base-package="com.infor.ion.boddesk.server.repositories" /> <context:property-placeholder location="classpath:jdbc.properties" /> <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource"> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.userName}" /> <property name="password" value="${jdbc.password}" /> </bean> </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="${jpa.database}" /> <property name="showSql" value="${jpa.showSql}" /> <property name="databasePlatform" value="${jpa.dialect}" /> </bean> </property> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> </property> <property name="persistenceUnitName" value="employeePU"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </beans>
The test succeeds but the record isn't stored in the database. When setting the debug logging on I noticed the following:Code:package com.infor.ion.boddesk.server.dao; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import com.infor.ion.boddesk.server.model.Employee; import com.infor.ion.boddesk.server.repositories.EmployeeRepository; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:persistContext.xml") @Transactional public class DaoTest { private static Logger logger = LoggerFactory.getLogger(DaoTest.class); @Autowired private EmployeeRepository dao; @Test public void testDao(){ logger.info("Test 'testDao' started ....."); Employee employee = new Employee(); employee.setAddress("Adress 1"); employee.setEmail("Email 1"); employee.setName("User name"); Employee savedUser = dao.save(employee); assertNotNull(savedUser.getId()); assertEquals(employee.getAddress(), savedUser.getAddress()); assertEquals(employee.getEmail(), savedUser.getEmail()); assertEquals(employee.getName(), savedUser.getName()); logger.info("Test 'testDao' done."); } }
13:45:06.287 [main] DEBUG org.hibernate.engine.jdbc.internal.LogicalConnecti onImpl - Obtained JDBC connection
13:45:06.287 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - initial autocommit status: true
13:45:06.287 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - disabling autocommit
13:45:06.289 [main] INFO com.infor.ion.boddesk.server.dao.DaoTest - Test 'testDao' started .....
13:45:06.307 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
13:45:06.327 [main] DEBUG org.hibernate.SQL - insert into Employee (address, dateOfBirth, designation, email, name, tenantId, version) values (?, ?, ?, ?, ?, ?, ?)
13:45:06.359 [main] DEBUG org.hibernate.id.IdentifierGeneratorHelper - Natively generated identity: 9
13:45:06.362 [main] INFO com.infor.ion.boddesk.server.dao.DaoTest - Test 'testDao' done.
13:45:06.363 [main] DEBUG org.hibernate.engine.transaction.spi.AbstractTrans actionImpl - rolling back
13:45:06.365 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - rolled JDBC Connection
13:45:06.365 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.Jdb cTransaction - re-enabling autocommit
Can anybody explain to me why this happens? And what I should do in the test class to make the record persistent?
Thanks in advance.


Reply With Quote