Spring and JPA - how to configure a transaction manager?
Could someone tell me how I enable a transaction when using Spring and JPA.
I followed the IBM tutorial:
http://www.ibm.com/developerworks/ed...spring2-i.html
and I can run JUnit tests (extends AbstractJpaTests) to store new java objects, but when I try to do
Code:
public static void main(String[] args) {
String[] contextPaths = new String[] {"com/test/jpa-spring2-service.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(contextPaths);
MessageDAO service = (MessageDAO)context.getBean("messageService");
Message message = new Message("test");
service.save(message);
}
No objects are persisted in the database. The findAll does work.
I use the following service code:
Code:
public class MessageDAO extends JpaDaoSupport implements MessageService {
public Message save(Message message) {
getJpaTemplate().persist(message);
return message;
}
}
I can get it to work by changing the service to:
Code:
public class MessageDAO extends JpaDaoSupport implements MessageService {
public Message save(Message message) {
getJpaTemplate().setEntityManager(getJpaTemplate().getEntityManagerFactory().createEntityManager());
getJpaTemplate().getEntityManager().getTransaction().begin();
getJpaTemplate().persist(message);
getJpaTemplate().getEntityManager().getTransaction().commit();
return message;
}
}
But this is of course not the way to go!
My com/test/jpa-spring2-service.xml is:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="messageService" class="com.test.MessageDAO">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="logRecordService" class="com.test.LogRecordDAO">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
</bean>
</property>
<property name="loadTimeWeaver">
<!-- <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/> -->
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost/xdb"/>
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
See also my question at:
http://forums.java.net/jive/thread.j...ssageID=228366
Could someone help me to configure a transaction? Or tell me what I do wrong? Thank you!