创建第三方 JPA 应用程序 jar 并将其作为库部署到 Payara

问题描述

我正在尝试构建第三方 JPA 模块(依赖 Payara 5 运行时的 Java 9 模块)并将其包装到 jar 中。

定义了persistence.xml并与一些实体一起放入meta-inf/persistence.xml中。

这些模块将访问在服务器上下文中定义的 payara 域 JDBC 资源(通过 JNDI 数据源)。

我的persistence.xml 看起来像:

<persistence version="2.2"
         xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <persistence-unit name="PG12_JTAPU" transaction-type="JTA">
    <jta-data-source>jdbc/PG12_ozssc_105</jta-data-source>
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.xxxx.Address</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        
    </properties>
  </persistence-unit>
</persistence>

这是jar文件中的文件结构(由emacs获取):

|- meta-inf/MANIFEST.MF
|- meta-inf/
|- com/
|- com/longz/
|- com/longz/thss/
|- com/longz/thss/thssappclient/
|- com/longz/thss/thssappclient/listener/
|- com/longz/thss/thssappclient/entity/
|- com/longz/thss/thssappclient/resources/
|- meta-inf/maven/
|- meta-inf/maven/com.longz.thss/
|- meta-inf/maven/com.longz.thss/ThssAppClient/
|- meta-inf/persistence.xml
|-  com/longz/thss/thssappclient/listener/ThssLifeCycleListener.class
|-  com/longz/thss/thssappclient/listener/ThssLifeCycleListener$1.class
|-  com/longz/thss/thssappclient/entity/Address.class
|-  com/longz/thss/thssappclient/entity/AddressBean.class
|-  com/longz/thss/thssappclient/resources/JavaEE8Resource.class
|-  com/longz/thss/thssappclient/JAXRSConfiguration.class
|-  meta-inf/maven/com.longz.thss/ThssAppClient/pom.xml
|-  meta-inf/maven/com.longz.thss/ThssAppClient/pom.properties

这是 AddressBean 的样子:

@ApplicationScoped
public class AddressBean implements Serializable {
  private static final ILogger ilogger = Logger.getLogger(AddressBean.class.getName());

  private static EntityManager em = null;
  private static Set<String> addressIds = null;

  @PersistenceContext(unitName = "PG12_JTAPU")
  private EntityManagerFactory emf;

  public AddressBean(){
    addressIds = new HashSet<>();
    if(emf != null){
        em = emf.createEntityManager();
    }else{
        ilogger.info("----------- Entity manager factory not found.");
    }
  }
  public Set<String> getAddressIds(){
    Set<String> stringSet = new HashSet<>();
    if (!em.isJoinedToTransaction()) {
        em.joinTransaction();
    }
    Query query = em.createNamedQuery("Address.findIDs",Address.class);
    stringSet= new HashSet<String>(query.getResultList());
    /*setKeysInDatabase(keys);*/
    em.flush();
    return stringSet;
  }
}

如果把这部分和一些必要的web.xml放到war文件里,部署到payara服务器,就可以正常启动了。这意味着 JPA 工作正常。

但是我将它作为lib添加到payara服务器,并调用其AddressBean类的方法访问entityManager,payara抛出错误显示

javax.persistence.PersistenceException: No Persistence provider for EntityManager named null

当我向下钻取时,我发现调用者或被调用者类(AddressBean)都没有找到 Persistence Provider。很可能 lib jar 内部类 AddressBean 没有在它们自己的 jar 上下文中读取 persistence.xml。

我的问题是:

有没有办法配置调用者类(其他服务器端类)或第三方Lib来访问包装在他们自己的jar包中的JPA?

或者在调用调用者类 AddressBean 时如何手动创建 JPA 上下文(PU、EntityManagerFactoru 或 EntityManager)。

或者这种方法根本不可用。

欢迎任何建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)