Is it possible to have Spring inject the JPA entityManager object into my DAO class without extending JpaDaoSupport? If yes, does Spring manage the transaction in this case?
I’m trying to keep my Spring configuration as simple as possible:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="em"/>
</bean>
<bean id="em" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPU"/>
</bean>
Yes, although it’s full of gotchas, since JPA is a bit peculiar. It’s very much worth reading the documentation on injecting JPA
EntityManagerandEntityManagerFactory, without explicit Spring dependencies in your code:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-jpa
This allows you to either inject the
EntityManagerFactory, or else inject a thread-safe, transactional proxy of anEntityManagerdirectly. The latter makes for simpler code, but means more Spring plumbing is required.