java @PersistenceUnit 注释不会创建 EntityManageFactory emf=null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4381724/
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 annotation won't create an EntityManageFactory emf=null
提问by jeff
I'm try to use the Sun Java PetStore Demo.
In the CatalogFacade class there is the following annotation:
我正在尝试使用 Sun Java PetStore Demo。
在 CatalogFacade 类中有以下注释:
@PersistenceUnit(unitName="myPetStorePU")
private EntityManagerFactory emf;
In all the methods of the CatalogFacade Sun has:
在 CatalogFacade Sun 的所有方法中都有:
EntityManager em = emf.createEntityManager();
But I am getting a null pointer exception for emf when trying to createEntityManager. But... if I add the following line above that line as such
但是我在尝试 createEntityManager 时收到了 emf 的空指针异常。但是...如果我在该行上方添加以下行
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("myPetStorePU");
EntityManager em = emf.createEntityManager();
then emf gets successfully created and the persistence unit myPetStorePU also successfully connects to the database. So it looks like persistence.xml syntax and its location is correct. I'd like to understand why the annotation doesn't work since I think there was a reason for just using the annotation as opposed to adding the createEntityManagerFactory line in every method.
然后 emf 成功创建,持久性单元 myPetStorePU 也成功连接到数据库。所以它看起来像persistence.xml 语法并且它的位置是正确的。我想了解为什么注释不起作用,因为我认为只使用注释而不是在每个方法中添加 createEntityManagerFactory 行是有原因的。
My src/META-INF/persistence.xml file looks like this:
我的 src/META-INF/persistence.xml 文件如下所示:
<persistence-unit name="myPetStorePU">
<description>Petstore Persistence Unit</description>
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>com.sun.javaee.blueprints.petstore.model.Tag</class>
<class>com.sun.javaee.blueprints.petstore.model.SellerContactInfo</class>
<class>com.sun.javaee.blueprints.petstore.model.Product</class>
<class>com.sun.javaee.blueprints.petstore.model.Item</class>
<class>com.sun.javaee.blueprints.petstore.model.Category</class>
<class>com.sun.javaee.blueprints.petstore.model.Address</class>
<class>com.sun.javaee.blueprints.petstore.model.ZipLocation</class>
<properties>
<property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="toplink.jdbc.url" value="jdbc:oracle:thin:@#############"/>
<property name="toplink.jdbc.user" value="####"/>
<property name="toplink.jdbc.password" value="#####"/>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
Edit: CatalogFacade is in the petstore.model package and implements the ServletContextListener
编辑:CatalogFacade 在 petstore.model 包中并实现了 ServletContextListener
<listener>
<listener-class>com.sun.javaee.blueprints.petstore.model.CatalogFacade</listener-class>
</listener>
in the index.jsp Sun has the following:
在 index.jsp Sun 中有以下内容:
<%
CatalogFacade cf = (CatalogFacade)config.getServletContext().getAttribute("CatalogFacade");
List<Tag> tags=cf.getTagsInChunk(0, 12);
%>
public List<Tag> getTagsInChunk(int start, int chunkSize) {
//The next line is required since the @PersistenceUnit annotation at the top of this class does not work
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("myPetStorePU");
EntityManager em = emf.createEntityManager();
System.out.println("Entity manager " + emf);
Query query = em.createQuery("SELECT t FROM Tag t ORDER BY t.refCount DESC, t.tag");
List<Tag> tags = query.setFirstResult(start).setMaxResults(chunkSize).getResultList();
em.close();
return tags;
}
回答by Bozho
If the annotated object is not managed by a container (either spring/CDI/EJB container), nothing gets injected into it.
如果带注释的对象不是由容器(spring/CDI/EJB 容器)管理的,则不会向其中注入任何内容。
So depending on your environment, obtain a contextualinstance of that object.
因此,根据您的环境,获取该对象的上下文实例。
If you are not using any of the above technologies (spring/CDI/EJB) - then you can't use @PersistenceUnit
and @PersistenceContext
. Use the manual way to obtain the unit.
如果您没有使用上述任何技术(spring/CDI/EJB) - 那么您不能使用@PersistenceUnit
and @PersistenceContext
。使用手动方式获取单元。
回答by ACV
For some reasons this happens when using this in you persistence.xml
由于某些原因,在您使用它时会发生这种情况 persistence.xml
<provider>org.hibernate.ejb.HibernatePersistence</provider>
switching to
切换到
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
fixes the problem
解决问题