问题
I have a JPA-Project, which I'm trying to update to JPA 2.2.0 and EclipseLink 5.7.1 since I ran into bug 429992 of EclipseLink. With the new versions in place, I'm not able to execute my application anymore – EclipseLink throws an exception similar to the following (Short variant from my example below):
[EL Warning]: metamodel: 2018-06-20 22:38:14.1--Thread(Thread[main,5,main])--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
[...]
Exception in thread "main" java.lang.IllegalArgumentException: Object: Artifact@17d919b6 is not a known Entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4324)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:593)
at Main.main(Main.java:12)
Before the update, everything worked fine (besides the aforementioned bug), and also if I checkout an earlier commit, there are no problems.
I have reproduced this behaviour with the minimal setup attached below.
The project is compiled using Java SE 10, as IDE I'm using Eclipse, but in my project properties, I only have the option to select "Generic 2.1" as JPA-Platform. May this be an problem?
Are you able to reproduce this error?
As far as I can see, the Entity-class is listed in the persistence.xml and also annotated with @Entity
, but not loaded by EclipseLink. Cleaning the project or even creating a new one does not solve the problem.
Do you have an idea, what my mistake might be? Am I missing any fundamental point about the usage of JPA 2.2/EclipseLink 2.7.1?
Thank you for any hints or comments!
main
method in the main class:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Example");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Artifact artifact = new Artifact();
entityManager.persist(artifact);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
Entity Artifact
:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
@Entity
@NamedQuery(name = "Artifact.findAll", query = "SELECT a FROM Artifact a")
public class Artifact {
private int id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}
persistence.xml:
<?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="Example">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>Artifact</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./inventory;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
</properties>
</persistence-unit>
</persistence>
pom.xml for Maven dependencies:
<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>
<groupId>JPATest</groupId>
<artifactId>JPATest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
</dependencies>
</project>
回答1:
I had the same problem too. finally, it was solved with an update in pom.xml.
Eclipselink 2.7.1 version has some bugs that fixed at newer versions.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.1</version>
</dependency>
to
<version>2.7.5</version>
before the update, entities are not loaded and em.getMetamodel() was empty. after the update, all of my entities are loaded successfully
issues and the fixed bugs can be accessed from this link.
回答2:
I have pretty much the same environment and experience the same problem. I hardly dare to say but in eclipse a "project/maven/update project ..." helped (so far).
来源:https://stackoverflow.com/questions/50957198/eclipselink-entities-not-loaded