If you have code similar to this:
public void persistDog(Dog dog) {
getJpaTemplate().persist(dog);
}
... and if nothing is persisted to database (and you don't see the insert in logs), don't panic. ;)
Enable transactions!
This will do the trick:
import org.springframework.transaction.annotation.Transactional;
// ...
@Transactional
public void persistDog(Dog dog) {
getJpaTemplate().persist(dog);
}
// ...
Your pom.xml file may need this:
...
<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.3.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.0.3.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>
...
<repository>
<id>r.j.o-groups-public</id>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
...
And your Spring beans file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
...
<tx:annotation-driven transaction-manager="myTransactionManager" />
<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
...
Enjoy.

2 comments: