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

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

问题描述

我正在尝试使用 Sun Java PetStore 演示.
在 CatalogFacade 类中有以下注释:

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;

在 CatalogFacade Sun 的所有方法中:

In all the methods of the CatalogFacade Sun has:

    EntityManager em = emf.createEntityManager();

但是我在尝试 createEntityManager 时收到了 emf 的空指针异常.但是...如果我在该行上方添加以下行

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();

然后 emf 成功创建,持久性单元 myPetStorePU 也成功连接到数据库.所以它看起来像persistence.xml 语法并且它的位置是正确的.我想了解为什么注释不起作用,因为我认为有理由只使用注释而不是在每个方法中添加 createEntityManagerFactory 行.

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.

我的 src/META-INF/persistence.xml 文件如下所示:

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>

CatalogFacade 位于 petstore.model 包中并实现了 ServletContextListener

CatalogFacade is in the petstore.model package and implements the ServletContextListener

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

在 index.jsp Sun 中有以下内容:

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

推荐答案

如果带注释的对象不是由容器(spring/CDI/EJB 容器)管理的,则不会注入任何内容.

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.

如果您没有使用上述任何技术(spring/CDI/EJB) - 那么您就不能使用 @PersistenceUnit@PersistenceContext.使用手动方式获取单位.

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天全站免登陆