I'have用WAR模塊,一些EJB模塊以及一些罐,例如一個EAR:實體未知與持久性單元EAR在另一個罐子
EAR
- moduleEjb.jar
- moduleWeb.war
-lib
- entity.jar
- resource.jar
我想使用持久性單位從資源。 jar,所以ejb模塊獨立於環境(我只能記住PU名稱,我可以在resource.jar中更改jndi而不更改moduleEjb.jar中的數據源),但是找不到實體。
的實體是在entity.jar:
@Entity
@Table(name = "ttracciatikettle", indexes = {...})
@NamedQueries({...})
public class TTracciatoKettle extends EntityBaseGest implements Serializable { ... }
持久性單元是在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>
和實體管理器被用來在moduleEjb.jar內部的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,我也會得到錯誤
[14,30]抽象模式類型'TTracciatoKettle'未知。
[39,47]狀態字段路徑't.codice'無法解析爲有效類型。
啓動持久單元myPU並且成功調用加載腳本。 如果我用同樣的PU但EJB模塊內部,它的工作原理
你用'@PersistenceContext(unitName =「lib/resoure.jar#myPU」)試過了嗎? – ujulu
@ujulu我剛剛試過,結果相同:( –