简体   繁体   English

EclipseLink:未加载实体

[英]EclipseLink: Entities not loaded

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 .我有一个 JPA 项目,我正尝试将其更新到 JPA 2.2.0 和 EclipseLink 5.7.1,因为我遇到了 EclipseLink 的错误 429992 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):新版本到位后,我无法再执行我的应用程序 – EclipseLink 抛出类似于以下内容的异常(以下示例中的简短变体):

[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.该项目是使用 Java SE 10 编译的,作为 IDE,我使用的是 Eclipse,但在我的项目属性中,我只能选择“Generic 2.1”作为 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.就我所见,实体类列在persistence.xml 中,也用@Entity注释,但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?我是否遗漏了有关使用 JPA 2.2/EclipseLink 2.7.1 的任何基本要点?

Thank you for any hints or comments!感谢您的任何提示或评论!


main method in the main class: main在主类方法:

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 :实体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:

<?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: Maven 依赖项的 pom.xml:

<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>

I had the same problem too.我也有同样的问题。 finally, it was solved with an update in pom.xml.最后,通过 pom.xml 中的更新解决了这个问题。

Eclipselink 2.7.1 version has some bugs that fixed at newer versions. Eclipselink 2.7.1 版本有一些错误已在较新版本中修复。

     <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.在更新之前,实体未加载并且 em.getMetamodel() 为空。 after the update, all of my entities are loaded successfully更新后,我所有的实体都成功加载

issues and the fixed bugs can be accessed from this link .可以从此链接访问问题和已修复的错误。

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).我几乎不敢说,但在 Eclipse 中,“项目/Maven/更新项目......”有所帮助(到目前为止)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM