@PersistenceUnit注释不会创建EntityManageFactory EMF = NULL [英] @PersistenceUnit annotation won't create an EntityManageFactory emf=null

查看:1779
本文介绍了@PersistenceUnit注释不会创建EntityManageFactory EMF = NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用了Sun Java的PetStore演示。结果
在CatalogFacade类有以下注释:

  @PersistenceUnit(的unitName =myPetStorePU)
私人EntityManagerFactory的电动势;

在CatalogFacade太阳的所有方法具有:

  EntityManager的EM = emf.createEntityManager();

但试图createEntityManager当我得到一个空指针异常的电动势。但是...如果我添加上面那行这样下面的行

  EntityManagerFactory的电动势= javax.persistence.Persistence.createEntityManagerFactory(myPetStorePU);
    EntityManager的EM = emf.createEntityManager();

然后EMF被成功创建并持久单元myPetStorePU也成功地连接到数据库。所以它看起来像的persistence.xml的语法和它的位置是否正确。我想明白为什么,因为我觉得是刚刚使用注释,而不是添加的createEntityManagerFactory线在每一个方法的理由注释不起作用。

我的src / META-INF / persistence.xml文件看起来是这样的:

 <持久性单位名称=myPetStorePU>
    <描述>的PetStore持久性单元< /描述>
    <提供商GT; oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider< /提供商GT;  <类别和GT; com.sun.javaee.blueprints.petstore.model.Tag< /班>
  <类别和GT; com.sun.javaee.blueprints.petstore.model.SellerContactInfo< /班>
  <类别和GT; com.sun.javaee.blueprints.petstore.model.Product< /班>
  <类别和GT; com.sun.javaee.blueprints.petstore.model.Item< /班>
  <类别和GT; com.sun.javaee.blueprints.petstore.model.Category< /班>
  <类别和GT; com.sun.javaee.blueprints.petstore.model.Address< /班>
  <类别和GT; com.sun.javaee.blueprints.petstore.model.ZipLocation< /班>
    <性状>
        <属性名=toplink.jdbc.driverVALUE =oracle.jdbc.driver.OracleDriver/>
        <属性名=toplink.jdbc.url值=的jdbc:神谕:薄:@ #############/>
        <属性名=toplink.jdbc.user值=####/>
        <属性名=toplink.jdbc.passwordVALUE =#####/>
        <属性名=toplink.logging.levelVALUE =INFO/>
    < /性状>< /持久化部>

编辑:
CatalogFacade在petstore.model包,并实现了ServletContextListener

 <听者GT;
    <监听级> com.sun.javaee.blueprints.petstore.model.CatalogFacade< /监听级>
< /听者GT;

在index.jsp的太阳有以下几点:

 <%
CatalogFacade CF =(CatalogFacade)config.getServletContext()的getAttribute(CatalogFacade);
清单<标记和GT;标记= cf.getTagsInChunk(0,12);
%GT;
公开名单<标记和GT; getTagsInChunk(INT开始,诠释CHUNKSIZE){
//因为在这个类的顶部@PersistenceUnit注释不起作用下一行是必需的
    EntityManagerFactory的电动势= javax.persistence.Persistence.createEntityManagerFactory(myPetStorePU);
    EntityManager的EM = emf.createEntityManager();
    的System.out.println(实体管理器+ EMF);
    查询查询= em.createQuery(选择T从标签T ORDER BY t.refCount DESC,t.tag);
    清单<标记和GT;标签= query.setFirstResult(开始).setMaxResults(CHUNKSIZE).getResultList();
    em.close();
    返回标签;
}


解决方案

如果被注释对象不是由一个容器(无论是春/ CDI / EJB容器)管理,没有被注入。

因此​​,根据环境的不同,获得的上下文的该对象的实例。

如果您没有使用任何上述技术(春/ CDI / EJB)的 - 那么你就不能使用 @PersistenceUnit @ PersistenceContext 。使用手动方式来获得单位。

I'm try to use the Sun Java PetStore Demo.
In the CatalogFacade class there is the following annotation:

@PersistenceUnit(unitName="myPetStorePU")
private EntityManagerFactory emf;

In all the methods of the CatalogFacade Sun has:

    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

    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.

My src/META-INF/persistence.xml file looks like this:

<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

<listener>
    <listener-class>com.sun.javaee.blueprints.petstore.model.CatalogFacade</listener-class>
</listener>

in the index.jsp Sun has the following:

<%
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;
}

解决方案

If the annotated object is not managed by a container (either spring/CDI/EJB container), nothing gets injected into it.

So depending on your environment, obtain a contextual instance 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.

这篇关于@PersistenceUnit注释不会创建EntityManageFactory EMF = NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆