Entity unknown in EAR with persistence unit in another jar

痴心易碎 提交于 2020-01-06 15:53:06

问题


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

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.

The entity is in entity.jar:

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

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>

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){
        ...
    }
}

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

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

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

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


回答1:


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>

istead of

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

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

Related reading: Chapter 8: Entity Packaging of JPA 2.1 specification.



来源:https://stackoverflow.com/questions/40016269/entity-unknown-in-ear-with-persistence-unit-in-another-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!