@OneToOne或@ManyToOne引用未知实体

时间:2015-01-28 17:04:30

标签: java entity-framework hibernate jpa entity-relationship

我试图在不同的jar中建立2个实体之间的关系。 这是主要项目中的第一个实体:

    @Entity(name = "StdyDtlLabelBean")
    @Table(name = "STDY_DTL_LABEL")
    public class StdyDtlLabelBean implements Serializable {
    @EmbeddedId
    private StdyDtlLabelBeanPk id;

    @ManyToOne(targetEntity = StdyDtlSubject.class)
    @JoinColumns({
        @JoinColumn(name="STUDY_ID", insertable = false, updatable = false, referencedColumnName="STUDY_ID"),
        @JoinColumn(name="SUBJECT_ID", insertable = false, updatable = false, referencedColumnName="SUBJECT_ID")
    })
    private StdyDtlSubject subject;
//getters and setters

}

这是一个在库项目中作为jar文件的实体:

@Entity
@Table(name = "STDY_DTL_SUBJECT")
public class StdyDtlSubject implements Serializable {

    private static final long serialVersionUID = 2479124604L;

    public StdyDtlSubject() {
    }

    @EmbeddedId
    private StdyDtlSubjectPK key;

//getters and setters
}

@Embeddable
public class StdyDtlSubjectPK implements Serializable {

    private static final long serialVersionUID = 6691432687933341920L;
    @Column(name = "STUDY_ID")
    private Integer studyId;
    @Column(name = "SUBJECT_ID")
    private String subjectId;

    public StdyDtlSubjectPK() {
    }

这是持久性单元:

<persistence-unit name="stdyPersistence" transaction-type="RESOURCE_LOCAL">
        <jta-data-source>java:comp/env/jdbc/OraclePooledDS</jta-data-source>
        <class>com.ctasc.ctpm.jpa.StdyDtlSubject</class>
        <class>stdy.brms.beans.StdyDtlLabelBean</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>

当我创建实体经理工厂时,我收到以下错误:

@OneToOne or @ManyToOne on stdy.brms.beans.StdyDtlLabelBean.subject references an unknown entity: com.ctasc.ctpm.jpa.StdyDtlSubject

但是如果我从StdyDtlLabelBean实体中删除关系注释并在StdyDtlSubject实体上创建命名查询。这些命名查询工作正常。另外,如果我将StdyDtlSubject类复制到我的主项目,它可以正常工作。只有在我们之间建立关系时才会出现此错误。

我已尝试添加

<jar-file>ctpm.jar</jar-file>
<jar-file>lib/ctpm.jar</jar-file>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="packagesToScan" value="com.ctasc.ctpm.jpa" /> 

他们都没有帮助我解决这个问题。你能否提出一些建议来解决这个问题?

1 个答案:

答案 0 :(得分:2)

确保要与之相关的jar中有一个persistence.xml,其中包含jar中所有实体的META-INF。