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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:08:16  来源:igfitidea点击:

PersistenceUnit vs PersistenceContext

javajakarta-eejpapersistenceentitymanager

提问by user2377971

In few project I have been successfully using

在我成功使用的几个项目中

@PersistenceUnit(unitName = "MiddlewareJPA")
EntityManagerFactory emf;
...
EntityManager entityManager = emf.createEntityManager();

to obtain EntityManagerfor Database connection, but some days ago I was trying to move my project to Jboss EAP 6.2and it couldn't create EntityManager. I was googling it and I found that I should try change @PersistenceUnitto

获取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 PersistenceUnitand PersistenceContext? What are pros and cons of each one? Where should we be using one of them?

获取EntityManager。它有效,但我不知道为什么。有什么区别bettweenPersistenceUnitPersistenceContext?各有什么优缺点?我们应该在哪里使用其中之一?

采纳答案by Alexey Andreev

I don't know how it works exactly in the Java EE, but in Spring, when you specify @PersistenceContextannotation, it injects EntityManager. Where does it get EntityManager? It is wrong to create one EntityManagerfor the whole application lifetime by calling EntityManagerFactory.createEntityManager(). So instead a special implementation of EntityManagerinterface 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 EMby 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. 它从哪里得到EntityManagerEntityManager通过调用为整个应用程序生命周期创建一个是错误的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 EntityManagerFactoryand @PersistenceUnityou should create/destroy EntityManagerevery time by hands and manage transactions too.

在一般情况下,最好注入EntityManager,因为EntityManagerFactory@PersistenceUnit你应该创建/销毁EntityManager用手每次和过于管理事务。

回答by chrylis -cautiouslyoptimistic-

PersistenceUnitinjects an EntityManagerFactory, and PersistenceContextinjects an EntityManager. It's generally better to use PersistenceContextunless you really need to manage the EntityManagerlifecycle manually.

PersistenceUnit注入一个EntityManagerFactory,然后PersistenceContext注入一个EntityManager。通常最好使用,PersistenceContext除非您确实需要EntityManager手动管理生命周期。

回答by Raj

EntityManagerobtained via @PersistenceContextis called Container Managed EntityManageras container will be responsible for managing "EntityManager". EntityManagerobtained 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等)。