EclipseLink:实体未加载

时间:2018-06-20 21:21:30

标签: java jpa eclipselink jpa-2.2

我有一个JPA项目,由于遇到bug 429992 of EclipseLink,我正尝试将其更新为JPA 2.2.0和EclipseLink 5.7.1。有了新版本后,我不再能够执行我的应用程序-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)

在更新之前,一切正常(除了上述错误),而且如果我签出更早的提交,也没有问题。

我通过以下最小设置重现了此行为。

该项目是使用Java SE 10编译的,因为我正在使用Eclipse的IDE,但是在我的项目属性中,我只能选择“通用2.1”作为JPA平台。可能有问题吗?

您能够重现此错误吗?

据我所知,Entity-class在persistence.xml中列出,并且还带有@Entity注释,但不是由EclipseLink加载的。清理项目甚至创建一个新项目都无法解决问题。

您有一个主意,我的错误可能是什么?我是否缺少有关使用JPA 2.2 / EclipseLink 2.7.1的基本要点?

感谢您的任何提示或评论!


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();

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

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

2 个答案:

答案 0 :(得分:0)

我也有同样的问题。最后,通过pom.xml中的更新解决了该问题。

Eclipselink 2.7.1版本具有一些在较新版本中修复的错误。

SELECT *
FROM links
WHERE link_date = CURDATE();

     <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.7.1</version>
    </dependency>

在更新之前,未加载实体,并且em.getMetamodel()为空。 更新后,我的所有实体均已成功加载

可以从this link访问

问题和已修复的错误。

答案 1 :(得分:-1)

我几乎拥有相同的环境,并遇到相同的问题。我几乎不敢说,但在日食方面,“项目/ maven /更新项目...”有所帮助(到目前为止)。