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>
You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet). You can use @PersistenceContext attribute unitName to specify a persistence unit by name, as Example 29-13 shows.
The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities. The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit.
You can give an object access to an EntityManager instance by using the @PersistenceUnit annotation to inject an EntityManagerFactory , from which you can obtain an EntityManager instance.
Yes, although it's full of gotchas, since JPA is a bit peculiar. It's very much worth reading the documentation on injecting JPA EntityManager and EntityManagerFactory, 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 an EntityManager directly. The latter makes for simpler code, but means more Spring plumbing is required.
Is it possible to have spring to inject the JPA entityManager object into my DAO class whitout extending JpaDaoSupport? if yes, does spring manage the transaction in this case?
This is documented black on white in 12.6.3. Implementing DAOs based on plain JPA:
It is possible to write code against the plain JPA without using any Spring dependencies, using an injected
EntityManagerFactoryorEntityManager. Note that Spring can understand@PersistenceUnitand@PersistenceContextannotations both at field and method level if aPersistenceAnnotationBeanPostProcessoris enabled. A corresponding DAO implementation might look like this (...)
And regarding transaction management, have a look at 12.7. Transaction Management:
Spring JPA allows a configured
JpaTransactionManagerto expose a JPA transaction to JDBC access code that accesses the same JDBC DataSource, provided that the registeredJpaDialectsupports retrieval of the underlying JDBC Connection. Out of the box, Spring provides dialects for the Toplink, Hibernate and OpenJPA JPA implementations. See the next section for details on theJpaDialectmechanism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With