在EAR中未知的实体,在另一个jar中具有持久性单元 [英] Entity unknown in EAR with persistence unit in another jar

查看:97
本文介绍了在EAR中未知的实体,在另一个jar中具有持久性单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个带有WAR模块,一些EJB模块和一些JARS的EAR:

I'have an EAR with a WAR module, some EJB modules and some JARS, for instance:

EAR
 - moduleEjb.jar
 - moduleWeb.war
 -lib
      - entity.jar
      - resource.jar

我想使用来自resource.jar的持久性单元,以便ejb模块独立于环境(我必须记住只有PU名称,我可以更改resource.jar中数据源的jndi,而无需更改moduleEjb.jar),但找不到实体.

I want to use persistence units from the resource.jar so the ejb modules are independent from the environment (i must remember only PU name, i can change the jndi for the datasource in resource.jar without change moduleEjb.jar), but the entities are not found.

该实体位于entity.jar中

The entity is in entity.jar:

@Entity
@Table(name = "ttracciatikettle", indexes = {...})
@NamedQueries({...})
public class TTracciatoKettle extends EntityBaseGest implements Serializable { ... }

持久性单元位于resource.jar

the persistence unit is in resource.jar

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:/jboss/datasource/mydata</jta-data-source>
    <jar-file>../entity.jar</jar-file>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="create"/>
        <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
        <property name="eclipselink.logging.level" value="ALL"/>
        <property name="eclipselink.logging.level.sql" value="ALL"/>
        <property name="javax.persistence.sql-load-script-source" value="META-INF/defaultdata.sql"/>
    </properties>
</persistence-unit>

实体管理器在EJB中的moduleEjb.jar中使用:

and the entity manager is used in moduleEjb.jar inside an EJB:

@PersistenceContext(unitName = "myPU")
private EntityManager em;

@Override
public TTracciatoKettle findByCodice(String codice) throws BadRequestException{
    try{
        //return this.em.createNamedQuery("TTracciatoKettle.findByCodice", entityClass)
        return this.em.createQuery("SELECT t FROM TTracciatoKettle t WHERE t.codice = :codice", entityClass)
                .setParameter("codice", codice)
                .getSingleResult();
    }catch(NoResultException ne){
        return null;
    }catch(NonUniqueResultException nure){
        ...
    }
}

即使我使用命名查询或jpql,我也会收到错误消息

Even if I use the named query or the jpql, i get the error

[14,30]抽象模式类型'TTracciatoKettle'是未知的.

[14, 30] The abstract schema type 'TTracciatoKettle' is unknown.

[39,47]状态字段路径't.codice'无法解析为有效类型.

[39, 47] The state field path 't.codice' cannot be resolved to a valid type.

启动持久性单元myPU,并且成功调用了加载脚本. 如果我在ejb模块中使用相同的PU,则可以正常工作

The persistence unit myPU is launched and also the load script is called successfully. If I use the same PU but inside the ejb module, it works

推荐答案

根据您发布的jars的目录结构,entity.jarresource.jar位于同一目录:lib.因此,persistence.xml文件中的jar-file条目应为:

According to the directory structure you posted the jars entity.jar and resource.jar are in the same directory: lib. So the jar-file entry in the persistence.xml file should be:

<jar-file>entity.jar</jar-file>

代替

<jar-file>../entity.jar</jar-file>

对于耳朵文件的lib目录中的持久性归档文件(在META-INF目录下的persistence.xml所在的jar),持久性单元myPU对耳朵中的所有组件均可见.这是经过稍微修改的目录布局:

With the persistence archive (the jar containing persistence.xml under META-INF directory) in the lib directory of the ear file, the persistence unit myPU should be visible to all components in the ear. And here is a slightly modified directory layout:

EAR
   moduleEjb.jar
   moduleWeb.war
   lib
      entity.jar
      resource.jar
          META-INF  // in the root of the jar file
              persistence.xml

相关阅读:第8章: JPA 2.1规范的实体包装.

这篇关于在EAR中未知的实体,在另一个jar中具有持久性单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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