通过@PersitenceContext或@PersitenceUnit注入EntityManagerFactory吗? [英] Inject a EntityManagerFactory through @PersitenceContext or @PersitenceUnit?
问题描述
我一直认为@PersistenceContext用于将EntityManager注入到容器管理的应用程序中,而@PersistenceUnit用于注入EntityManagerFactory.
I've always thought that @PersistenceContext was for injecting EntityManager into a container-managed application, while @PersistenceUnit was for injecting an EntityManagerFactory.
Javadoc说
对于PersistenceUnit( http://docs.oracle.com/javaee/7/api/javax/persistence/PersistenceUnit.html )
For PersistenceUnit (http://docs.oracle.com/javaee/7/api/javax/persistence/PersistenceUnit.html)
表示对EntityManagerFactory及其关联的持久性单元的依赖.
Expresses a dependency on an EntityManagerFactory and its associated persistence unit.
对于PersistenceContext( http://docs.oracle.com/javaee/7/api/javax/persistence/PersistenceContext.html )
And for PersistenceContext (http://docs.oracle.com/javaee/7/api/javax/persistence/PersistenceContext.html)
表示对容器管理的EntityManager及其关联的持久性上下文的依赖.
Expresses a dependency on a container-managed EntityManager and its associated persistence context.
到目前为止,一切都很好,但是随后我正在阅读JPA教程(请参见 https://docs.oracle.com/cd/E19798-01/821-1841/bnbqy/index.html ),其中包含类似的示例
So far so good, but then I was reading the JPA tutorial (see https://docs.oracle.com/cd/E19798-01/821-1841/bnbqy/index.html) that contains an example like this
以下示例显示了如何在使用应用程序管理的实体管理器的应用程序中管理事务:
The following example shows how to manage transactions in an application that uses an application-managed entity manager:
@PersistenceContext
EntityManagerFactory emf;
EntityManager em;
@Resource
UserTransaction utx;
...
em = emf.createEntityManager();
try {
utx.begin();
em.persist(SomeEntity);
em.merge(AnotherEntity);
em.remove(ThirdEntity);
utx.commit();
} catch (Exception e) {
utx.rollback();
}
如果我们在谈论应用程序托管代码,那么PersistenceContext也可以引用EntityManagerFactory吗?
so the PersistenceContext can also refer to an EntityManagerFactory if we're talking about an application managed code?
免责声明-与我猜出的这个问题的答案无关- PersistenceUnit vs PersistenceContext
disclaimer -- not related to the answers from this question I guess -- PersistenceUnit vs PersistenceContext
推荐答案
我一直认为@PersistenceContext用于将EntityManager注入到容器管理的应用程序中,而@PersistenceUnit用于注入EntityManagerFactory.
I've always thought that @PersistenceContext was for injecting EntityManager into a container-managed application, while @PersistenceUnit was for injecting an EntityManagerFactory.
是的.
我想JPA教程的示例是一个粗心的错误.先前在应用程序管理的实体管理器"的同一部分中,其编写为
I guess the example of the JPA tutorial is a careless mistake. Previously in the same section 'Application-Managed Entity Managers' it's written
要获取EntityManager实例,首先必须通过使用javax.persistence.PersistenceUnit批注将其注入到应用程序组件中来获得EntityManagerFactory实例:
To obtain an EntityManager instance, you first must obtain an EntityManagerFactory instance by injecting it into the application component by means of the javax.persistence.PersistenceUnit annotation:
@PersistenceUnitEntityManagerFactory emf;
@PersistenceUnit EntityManagerFactory emf;
然后从EntityManagerFactory实例获取EntityManager:
Then obtain an EntityManager from the EntityManagerFactory instance:
EntityManager em = emf.createEntityManager();
EntityManager em = emf.createEntityManager();
这篇关于通过@PersitenceContext或@PersitenceUnit注入EntityManagerFactory吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!