簡體   English   中英

EclipseLink:未加載實體

[英]EclipseLink: Entities not loaded

我有一個 JPA 項目,我正嘗試將其更新到 JPA 2.2.0 和 EclipseLink 5.7.1,因為我遇到了 EclipseLink 的錯誤 429992 新版本到位后,我無法再執行我的應用程序 – 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 編譯的,作為 IDE,我使用的是 Eclipse,但在我的項目屬性中,我只能選擇“Generic 2.1”作為 JPA-Platform。 這可能是個問題嗎?

你能重現這個錯誤嗎?

就我所見,實體類列在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;
    }
}

持久性.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>

我也有同樣的問題。 最后,通過 pom.xml 中的更新解決了這個問題。

Eclipselink 2.7.1 版本有一些錯誤已在較新版本中修復。

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

 <version>2.7.5</version>

在更新之前,實體未加載並且 em.getMetamodel() 為空。 更新后,我所有的實體都成功加載

可以從此鏈接訪問問題和已修復的錯誤。

我有幾乎相同的環境並遇到相同的問題。 我幾乎不敢說,但在 Eclipse 中,“項目/Maven/更新項目......”有所幫助(到目前為止)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM