Java PersistenceUnit 与 PersistenceContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21038706/
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
PersistenceUnit vs PersistenceContext
提问by user2377971
In few project I have been successfully using
在我成功使用的几个项目中
@PersistenceUnit(unitName = "MiddlewareJPA")
EntityManagerFactory emf;
...
EntityManager entityManager = emf.createEntityManager();
to obtain EntityManager
for Database connection, but some days ago I was trying to move my project to Jboss EAP 6.2
and it couldn't create EntityManager
. I was googling it and I found that I should try change @PersistenceUnit
to
获取EntityManager
数据库连接,但几天前我试图将我的项目移动到Jboss EAP 6.2
它无法创建EntityManager
. 我在谷歌上搜索它,我发现我应该尝试更改@PersistenceUnit
为
@PersistenceContext(unitName = "MiddlewareJPA")
private EntityManager entityManager;
to obtain EntityManager. It worked but I don't know why. What is the difference bettween PersistenceUnit
and PersistenceContext
? What are pros and cons of each one? Where should we be using one of them?
获取EntityManager。它有效,但我不知道为什么。有什么区别bettweenPersistenceUnit
和PersistenceContext
?各有什么优缺点?我们应该在哪里使用其中之一?
采纳答案by Alexey Andreev
I don't know how it works exactly in the Java EE, but in Spring, when you specify @PersistenceContext
annotation, it injects EntityManager
. Where does it get EntityManager
? It is wrong to create one EntityManager
for the whole application lifetime by calling EntityManagerFactory.createEntityManager()
. So instead a special implementation of EntityManager
interface is used and instantiated directly. It has an internal mutable thread-local reference to a realEntityManager
. Implementations of methods just redirect calls to this realEntityManager
. And there is a servlet listener, that before each request obtain EM
by calling EMF.createEntityManager()
and assign it to that inner reference of special EM
. Also this listener manages transactions by calling getTransaction().begin()
, .commit()
and .rollback()
on the realEM
. It is very simplified description of performed work. And I believe, that JEE container does the same thing, as Spring does.
我不知道它在 Java EE 中是如何工作的,但是在 Spring 中,当您指定@PersistenceContext
注释时,它会注入EntityManager
. 它从哪里得到EntityManager
?EntityManager
通过调用为整个应用程序生命周期创建一个是错误的EntityManagerFactory.createEntityManager()
。所以取而代之的EntityManager
是使用接口的特殊实现并直接实例化。它有一个对真实EntityManager
. 方法的实现只是将调用重定向到这个真正的EntityManager
. 并且有一个 servlet 侦听器,在每个请求之前EM
通过调用获取EMF.createEntityManager()
并将其分配给 special 的内部引用EM
。此外,此侦听器通过调用getTransaction().begin()
,.commit()
并.rollback()
在实际中管理事务EM
. 这是对已执行工作的非常简化的描述。而且我相信,JEE 容器和 Spring 做同样的事情。
In general case it is better to inject EntityManager
, because with EntityManagerFactory
and @PersistenceUnit
you should create/destroy EntityManager
every time by hands and manage transactions too.
在一般情况下,最好注入EntityManager
,因为EntityManagerFactory
和@PersistenceUnit
你应该创建/销毁EntityManager
用手每次和过于管理事务。
回答by chrylis -cautiouslyoptimistic-
PersistenceUnit
injects an EntityManagerFactory
, and PersistenceContext
injects an EntityManager
. It's generally better to use PersistenceContext
unless you really need to manage the EntityManager
lifecycle manually.
PersistenceUnit
注入一个EntityManagerFactory
,然后PersistenceContext
注入一个EntityManager
。通常最好使用,PersistenceContext
除非您确实需要EntityManager
手动管理生命周期。
回答by Raj
EntityManager
obtained via @PersistenceContext
is called Container Managed EntityManager
as container will be responsible for managing "EntityManager
". EntityManager
obtained via @PersistenceUnit
/ entityManagerFactory.createEntityManager()
is managed in the application by the developer. (for e.g. managing lifecycle of EntityManager
, releasing the resources acquired by EntityManager
, etc.).
EntityManager
通过获得@PersistenceContext
的称为容器管理,EntityManager
因为容器将负责管理“ EntityManager
”。EntityManager
通过@PersistenceUnit
/获得entityManagerFactory.createEntityManager()
的由开发者在应用程序中管理。(例如管理 的生命周期EntityManager
,释放 获得的资源EntityManager
等)。