Java 如何使用 spring 注入 JPA EntityManager
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2421339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to inject JPA EntityManager using spring
提问by marcosbeirigo
Is it possible to have Springinject the JPA entityManager
object into my DAO class without extending JpaDaoSupport
? If yes, does Spring manage the transaction in this case?
是否可以让Spring在entityManager
不扩展的情况下将JPA对象注入到我的 DAO 类中JpaDaoSupport
?如果是,在这种情况下 Spring 会管理事务吗?
I'm trying to keep my Spring configuration as simple as possible:
我试图让我的 Spring 配置尽可能简单:
<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>
采纳答案by skaffman
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:
是的,尽管它充满了陷阱,因为 JPA 有点奇怪。非常值得阅读有关注入 JPAEntityManager
和的文档EntityManagerFactory
,在您的代码中没有显式的 Spring 依赖项:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-jpa
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.
这允许您注入EntityManagerFactory
,或者EntityManager
直接注入 的线程安全事务代理。后者使代码更简单,但意味着需要更多的 Spring 管道。
回答by Pascal Thivent
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?
是否可以在不扩展 JpaDaoSupport 的情况下让 spring 将 JPA entityManager 对象注入到我的 DAO 类中?如果是,在这种情况下 spring 是否管理事务?
This is documented black on white in 12.6.3. Implementing DAOs based on plain JPA:
这在12.6.3 中有记录。基于普通 JPA 实现 DAO:
It is possible to write code against the plain JPA without using any Spring dependencies, using an injected
EntityManagerFactory
orEntityManager
. Note that Spring can understand@PersistenceUnit
and@PersistenceContext
annotations both at field and method level if aPersistenceAnnotationBeanPostProcessor
is enabled. A corresponding DAO implementation might look like this (...)
可以在不使用任何 Spring 依赖项的情况下,使用注入的
EntityManagerFactory
或EntityManager
. 请注意,如果 启用了 a ,Spring 可以在字段和方法级别理解@PersistenceUnit
和@PersistenceContext
注释PersistenceAnnotationBeanPostProcessor
。相应的 DAO 实现可能如下所示 (...)
And regarding transaction management, have a look at 12.7. Transaction Management:
关于事务管理,请查看12.7。交易管理:
Spring JPA allows a configured
JpaTransactionManager
to expose a JPA transaction to JDBC access code that accesses the same JDBC DataSource, provided that the registeredJpaDialect
supports 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 theJpaDialect
mechanism.
Spring JPA 允许配置
JpaTransactionManager
为将 JPA 事务暴露给访问相同 JDBC 数据源的 JDBC 访问代码,前提是注册的JpaDialect
支持检索底层 JDBC 连接。Spring 开箱即用地为 Toplink、Hibernate 和 OpenJPA JPA 实现提供方言。有关该JpaDialect
机制的详细信息,请参阅下一节。
回答by Denis Skarbichev
The latest Spring + JPA versions solve this problem fundamentally. You can learn more how to use Spring and JPA togather in a separate thread
最新的 Spring + JPA 版本从根本上解决了这个问题。您可以在单独的线程中了解更多如何同时使用 Spring 和 JPA