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
No objects are persisted in the database. The findAll does work.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); }
I use the following service code:
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().persist(message); return message; } }
But this is of course not the way to go!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; } }
My com/test/jpa-spring2-service.xml is:
See also my question at: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>
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!


Reply With Quote