Java Reference
In-Depth Information
@Transactional(readOnly = true)
public List<Course> findAll() {
Query query = entityManager.createQuery("from Course");
return query.getResultList();
}
}
You can annotate each DAO method or the entire DAO class with @Transactional to make
all these methods transactional. It ensures that the persistence operations within a DAO method
will by executed in the same transaction and hence by the same entity manager.
In the bean configuration file for JPA (i.e., beans-jpa.xml ), you have to declare a
JpaTransactionManager instance and enable declarative transaction management via
<tx:annotation-driven> . You have to register a PersistenceAnnotationBeanPostProcessor
instance to inject entity managers into properties annotated with @PersistenceContext .
<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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
...
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean name="courseDao"
class="com.apress.springenterpriserecipes.course.jpa.JpaCourseDao" />
<bean class="org.springframework.orm.jpa.support.
PersistenceAnnotationBeanPostProcessor" />
</beans>
A PersistenceAnnotationBeanPostProcessor instance will be registered automatically once you
enable the <context:annotation-config> element. So, you can delete its explicit bean declaration.
<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"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
Search WWH ::




Custom Search