Jpa 复合持久化单元的元模型为空
当我创建复合持久性单元并尝试使用criteria API时,我收到以下异常消息:Jpa 复合持久化单元的元模型为空,jpa,glassfish,eclipselink,payara,Jpa,Glassfish,Eclipselink,Payara,当我创建复合持久性单元并尝试使用criteria API时,我收到以下异常消息: java.lang.IllegalArgumentException: No [EntityType] was found for the key class [nl.example.application.datalayer.entity.db.EntityA] in the Metamodel - please verify that the [Entity] class was referenced i
java.lang.IllegalArgumentException: No [EntityType] was found for the key class [nl.example.application.datalayer.entity.db.EntityA] in the Metamodel - please verify that the [Entity] class was referenced in persistence.xml using a specific <class>nl.example.application.datalayer.entity.db.EntityA</class> property or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element.
我改为:
private EntityManagerFactory emf;
private EntityManager em;
@PostConstruct
public void initialize() {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("eclipselink.classloader", this.getClass().getClassLoader());
hashMap.put("eclipselink.composite-unit", "true");
hashMap.put("eclipselink.deploy-on-startup", "true");
em = Persistence.createEntityManagerFactory("CompositePu", hashMap).createEntityManager();
if (metamodel.getManagedTypes().isEmpty()) {
logger.log(Level.WARNING, "meta model is empty");
}
私有实体管理器工厂emf;
私人实体管理者;
@施工后
公共无效初始化(){
HashMap HashMap=新的HashMap();
put(“eclipselink.classloader”,this.getClass().getClassLoader());
hashMap.put(“日食合成单位”、“真”);
put(“eclipselink.DeployonStartup”,“true”);
em=Persistence.createEntityManager工厂(“CompositePu”,hashMap).createEntityManager();
if(metamodel.getManagedTypes().isEmpty()){
logger.log(Level.WARNING,“元模型为空”);
}
不幸的是,元模型仍然为空,上面的日志行被触发
1.本机查询不需要找到类。打开日志记录,查看部署持久性单元时发现了什么:还要检查一个简单的JPQL查询是否有效,因为用于持久性单元的加载程序可能与应用程序使用的加载程序不同,这可能是类加载问题。谢谢你的建议。我有我的建议n正确编写了本机查询,我指的是JPQL查询。有效的查询是由以下代码创建的:em.createQuery(“从EntityA e中选择e,其中e.dummyValue=:dummyValue”,EntityA.class)。类被加载,即在MetaModelImpl.initialize(ClassLoader ClassLoader)中加载方法在eclipselink中,我看到应用程序类以及包含实体的jar都已加载。因此,这似乎不是类加载问题。MetaModelImpl来自何处?我找不到接受类加载器的初始化方法,因此我不知道该如何工作。请尝试createQuery(“从EntityA中选择e”)请看,因为我不认为您传入的EntityA.class与加载持久性单元的加载程序是同一个加载程序。创建工厂时,可以使用属性在EclipseLink中设置类加载程序,事实上它们看起来是不同的类加载程序。@Chris:我根据您的建议修改了包含持久性单元的bean中的代码estion,不幸的是,它没有帮助。还有什么想法吗?谢谢你的帮助!本机查询不需要找到类。打开日志记录,查看部署持久性单元时发现了什么:还要检查一个简单的JPQL查询是否有效,因为加载程序用于持久性单元时可能会出现类加载问题与您的应用程序使用的不同。感谢您的建议。我编写的本机查询不正确,而我指的是JPQL查询。有效的查询是由以下代码创建的:em.createQuery(“从EntityA e中选择e,其中e.dummyValue=:dummyValue”,EntityA.class)。类已加载,即在MetaModelImpl.initialize中加载(ClassLoader ClassLoader)方法在eclipselink中,我看到应用程序类以及包含实体的jar都已加载。因此,这似乎不是类加载问题。MetaModelImpl来自何处?我找不到接受类加载程序的初始化方法,因此我不知道该方法如何工作。请尝试createQuery(“从实体A中选择e”)请看,因为我不认为您传入的EntityA.class与加载持久性单元的加载程序是同一个加载程序。创建工厂时,可以使用属性在EclipseLink中设置类加载程序,事实上它们看起来是不同的类加载程序。@Chris:我根据您的建议修改了包含持久性单元的bean中的代码很遗憾,这没有帮助。还有什么想法吗?谢谢你的帮助!
Example
|--- datalayer_project)
| |--- datalayer_parent
| | |--- pom.xml
| |
| |--- entity (some jar with entities)
| | |--- src
| | | |---main
| | | |--- java
| | | |--- resources
| | | |--- META-INF/persistence.xml
| | |--- pom.xml
| |
| |--- datalayer (the composite persistence unit to be used by the application)
| | |--- src
| | | |---main
| | | |--- java
| | | |--- resources
| | | |--- META-INF/persistence.xml
| | |--- build-jar-with-dependencies.xml (maven-assembly-plugin descriptor to build jar with all entites and their metamodel classes)
| | |--- pom.xml
| |--- pom.xml
|
|--- application (this is the example application which will be deployed)
| |--- src
| | |--- <source code>
| |--- pom.xml
|
|--- pom.xml
|--- setupServer.sh (script to create GlassFish/Payara domain)
<?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="examplePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/DataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.composite-unit.member" value="true"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.target-database" value="PostgreSQL"/>
<property name="eclipselink.deploy-on-startup" value="true" />
</properties>
</persistence-unit>
<?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="CompositePu" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jar-file>entity-${project.version}.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.composite-unit" value="true" />
<property name="eclipselink.logging.parameters" value="true" />
<property name="eclipselink.deploy-on-startup" value="true" />
</properties>
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nl.example.application</groupId>
<artifactId>application_project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>example</artifactId>
<packaging>war</packaging>
<name>example</name>
<dependencies>
<dependency>
<groupId>nl.example.application.datalayer</groupId>
<artifactId>datalayer</artifactId>
<version>${project.version}</version>
<classifier>jar-with-dependencies</classifier>
</dependency>
<dependency>
<groupId>de.danielbechler</groupId>
<artifactId>java-object-diff</artifactId>
<version>0.94</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.4.3.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>2.4.3.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>example</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<classpathPrefix>example/WEB-INF/lib</classpathPrefix>
</manifest>
<manifestEntries>
<git-SHA-1>${buildNumber}</git-SHA-1>
</manifestEntries>
</archive>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>true</filtering>
<includes>
<include>index.xhtml</include>
</includes>
</webResource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/business/util/control/Settings**</exclude>
<exclude>**/*_*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<scm>
<connection>scm:git:git@github.com:JCGreiner/JPACompositeAndMetaModel.git</connection>
<tag>HEAD</tag>
</scm>
</project>
@PersistenceContext(unitName = "CompositePu")
protected EntityManager em;
private EntityManagerFactory emf;
private EntityManager em;
@PostConstruct
public void initialize() {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("eclipselink.classloader", this.getClass().getClassLoader());
hashMap.put("eclipselink.composite-unit", "true");
hashMap.put("eclipselink.deploy-on-startup", "true");
em = Persistence.createEntityManagerFactory("CompositePu", hashMap).createEntityManager();
if (metamodel.getManagedTypes().isEmpty()) {
logger.log(Level.WARNING, "meta model is empty");
}