【问题标题】:EclipseLink cannot find entity in OneToMany relationshipEclipseLink 在 OneToMany 关系中找不到实体
【发布时间】:2013-10-10 12:57:15
【问题描述】:

我有一个非常简单的 eclipse 链接案例,我在下面复制了它。如果我可以让下面的示例正常工作,那么我可以轻松地将它应用到有问题的实际案例中。问题是,我不能!

有两个类 - ParentChild。两者都有String id,Parent 包含Child 字段children 中的对象列表。孩子的List 具有@OneToMany 属性,并且两个类都具有@Entity 属性。我通过 Eclipse 作为 Maven 项目运行所有内容,persistence.xml 存储在src\main\resources\META_INF\persistence.xml 中。没有引用其他 jars。

当我调用Persistence.createEntityManagerFactory 时,我得到以下异常。好像认为Child不是实体!

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@1a8e3115
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [Parent] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.test.Parent] uses a non-entity [class com.test.Child] as target entity in the relationship attribute [field children].
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at com.test.Main.main(Main.java:22)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [Parent] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.test.Parent] uses a non-entity [class com.test.Child] as target entity in the relationship attribute [field children].
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1950)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1941)
    at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:96)
    ... 3 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [Parent] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.test.Parent] uses a non-entity [class com.test.Child] as target entity in the relationship attribute [field children].
    at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:230)
    ... 7 more
Caused by: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class com.test.Parent] uses a non-entity [class com.test.Child] as target entity in the relationship attribute [field children].
    at org.eclipse.persistence.exceptions.ValidationException.nonEntityTargetInRelationship(ValidationException.java:1378)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.getReferenceDescriptor(RelationshipAccessor.java:568)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.RelationshipAccessor.processJoinTable(RelationshipAccessor.java:725)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.OneToManyAccessor.processManyToManyMapping(OneToManyAccessor.java:198)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.OneToManyAccessor.process(OneToManyAccessor.java:147)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processOwningRelationshipAccessors(MetadataProject.java:1578)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1831)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:580)
    at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:585)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1865)
    ... 5 more

我在这方面尝试了很多东西,但我真的不知道接下来要尝试什么。我认为我做错了可怕的事情,但谷歌终于 exauhsted 自己。非常感谢任何帮助:)

所有要复制的相关文件如下:

家长:

package com.test;

import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Parent {
    @Id
    private String      id;
    @OneToMany
    private List<Child> children;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Child> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }
}

孩子:

package com.test;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Child {
    @Id
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

persistence.xml:

<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.

    <persistence-unit name="Parent">    
        <class>com.test.Parent</class>      
        <properties>        
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>      
            <property name="eclipselink.jdbc.read-connections.min" value="1" />
            <property name="eclipselink.jdbc.write-connections.min" value="1" />
            <property name="eclipselink.jdbc.batch-writing" value="JDBC" />         
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
            <!-- Logging -->
            <property name="eclipselink.logging.level" value="INFO" />
            <property name="eclipselink.logging.timestamp" value="true" />
            <property name="eclipselink.logging.session" value="true" />
            <property name="eclipselink.logging.thread" value="false" />                                
        </properties>       
    </persistence-unit> 

    <persistence-unit name="Child"> 
        <class>com.test.Child</class>       
        <properties>        
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>      
            <property name="eclipselink.jdbc.read-connections.min" value="1" />
            <property name="eclipselink.jdbc.write-connections.min" value="1" />
            <property name="eclipselink.jdbc.batch-writing" value="JDBC" />         
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
            <!-- Logging -->
            <property name="eclipselink.logging.level" value="INFO" />
            <property name="eclipselink.logging.timestamp" value="true" />
            <property name="eclipselink.logging.session" value="true" />
            <property name="eclipselink.logging.thread" value="false" />                                
        </properties>       
    </persistence-unit> 


</persistence>

主类(用于测试):

package com.test;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.Persistence;

public class Main {
    private static final String KEY_PERSISTENCE_URL  = "javax.persistence.jdbc.url";
    private static final String KEY_PERSISTENCE_USER = "javax.persistence.jdbc.user";
    private static final String KEY_PERSISTENCE_PASS = "javax.persistence.jdbc.password";

    public static void main(String[] args) {
        Map<String, String> properties = new HashMap<>();
        properties.put(KEY_PERSISTENCE_URL, "jdbc:mysql://localhost:3306/reporting");
        properties.put(KEY_PERSISTENCE_USER, "root");
        properties.put(KEY_PERSISTENCE_PASS, "p4ssw0rd");
        Persistence.createEntityManagerFactory(Parent.class.getSimpleName(), properties);
    }
}

【问题讨论】:

    标签: java jpa persistence eclipselink


    【解决方案1】:

    正在使用的持久化单元只包含一个实体,因此不知道如何持久化引用的非实体子类。 Child 是另一个持久化单元中的一个实体,但它只是父 PU 的一个 java 对象。这两个单元应该合并为一个包含所有依赖实体的单元

    【讨论】:

    • 我不知道多个类应该包含在同一个持久性单元中,所以这看起来像是我错过的(必须等到明天才能测试,可能是错误的)。如果Child 对象包含在Parent School 对象中(例如),那么该类是否可以存在于两个单独的持久性单元中? (假设我首先想要两个单独的“父母”和“学校”单元)。
    • 您的实体可以根据需要在任意多个持久性单元中。它只是一个java类。持久化单元是存储机制。我不明白为什么要将父母和学校分开,除非您将它们放在不同的数据库中,并且两个数据库实例都有自己的子表示,允许您重用子类。
    • 这行得通 - 我知道我错过了一些基本的东西,所以谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多