【问题标题】:EntityManager is always null on REST project (netbeans) running on weblogic 12c在 weblogic 12c 上运行的 REST 项目 (netbeans) 上,EntityManager 始终为空
【发布时间】:2017-08-07 22:24:45
【问题描述】:

我一直在 Netbeans 8.2(使用 Java 1.7 编译)上开发一个 REST 项目。我正在 Weblogic Server 12.1.2 上部署我的 Web 项目,并使用 JPA(带 Eclipselink)作为引用服务器上配置的 JTA 数据源的持久性引擎。

我在谷歌调查的问题很常见;但是,我无法在网上找到任何可以帮助我解决此问题的解决方案。

基本上,这就是我的persistence.xml 文件的定义方式:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="gchPermissionPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc.vz.customer.ds</jta-data-source>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.target-server" value="WebLogic"/>
      <property name="javax.persistence.query.timeout" value="660000"/>
      <property name="eclipselink.logging.level" value="ALL"/>
      <property name="eclipselink.persistence-context.flush-mode" value="COMMIT"/>
      <property name="eclipselink.cache.shared.default" value="false"/>
    </properties>
  </persistence-unit>
</persistence>

这就是我在课堂上使用EntityManager 的方式:

@PersistenceContext(unitName = "gchPermissionPU")
protected EntityManager entityManager;

每当调用此行时,我都会收到 NullPointerException(我现在唯一引用 entityManager 的行):

Query permissionsQuery = entityManager.createNativeQuery(nativeSqlQuery.toString());

我相信 NullPointer 出现是因为 entityManager 变量为空。

这是我的 weblogic 上的基本数据源配置:

我想知道我是否需要在 weblogic 端进行某种额外的配置或缺少任何代码。

提前感谢您的时间和帮助。任何线索/反馈表示赞赏。

【问题讨论】:

    标签: java jpa datasource weblogic12c


    【解决方案1】:

    我设法通过手动创建EntityManagerFactoryEntityManager 来解决我的问题,来自这个答案:

    PersistenceContext EntityManager injection NullPointerException

    似乎没有 EJB 引用,您需要强制创建持久性组件。所以我的团队想出了以下解决方案:

    //1.定义持久化所需的组件:

    @PersistenceUnit(unitName = "gchPermissionPU")
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;
    

    //2.如果它是“null”,则只是一个创建实体管理器工厂的方法

    public final EntityManager getEntityManager() {
        if (entityManagerFactory == null) {
            entityManagerFactory = Persistence.createEntityManagerFactory("gchPermissionPU");
        }
        if (entityManager == null) {
            entityManager = entityManagerFactory.createEntityManager();
        }
    
        return entityManager;
    }
    

    但是,我想知道这对于将由 > 1000 个用户使用的 Web 应用程序是否是最佳解决方案。我不敢相信一个 Web 应用程序应该依赖 EJB 进行注入(可能是因为它是 Weblogic),但我记得我前段时间在 Tomcat 上实现了一个类似的应用程序,不需要做这个“解决方法”。

    如果有人有其他 cmets 或反馈,请告诉我。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多