无法在OneToMany关系上确定关系属性的目标实体

时间:2013-11-15 08:04:48

标签: hibernate jpa netbeans netbeans-7

我在创建RESTFul Web服务时使用NetBeans 7.4生成的Entity类代码。 当我测试webservice时,我得到以下异常

 The target entity of the relationship attribute [emps] on the class 
 [class test.Dept] cannot be determined.  When not using generics, ensure 
 the target entity is defined on the relationship mapping.

Emp Entity

@Entity
@Table(name="EMP"
    ,schema="SCOTT"
)@XmlRootElement

public class Emp  implements java.io.Serializable {

@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="DEPTNO")
    public Dept getDept() {
        return this.dept;
    }

部门实体

@Entity
@Table(name="DEPT"
    ,schema="SCOTT"
)@XmlRootElement

public class Dept  implements java.io.Serializable {

@OneToMany(fetch=FetchType.LAZY, mappedBy="dept")
    public Set getEmps() {
        return this.emps;
    }

hibernate.reveng

<?xml version="1.0" encoding="UTF-8"?>    
<hibernate-reverse-engineering>
  <schema-selection match-schema="SCOTT"/>
  <table-filter match-name="EMP"/>
  <table-filter match-name="DEPT"/>
</hibernate-reverse-engineering>

hibernate.cfg

 <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property 
    name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <mapping class="test.Emp"/>
    <mapping class="test.Dept"/>
  </session-factory>

1 个答案:

答案 0 :(得分:1)

emps关系未提供足够的信息来确定目标实体。您有两种简单的方法来提供这些信息。

A)泛型

使用泛型(可能意味着一些重构):

@OneToMany(fetch=FetchType.LAZY, mappedBy="dept")
public Set<Emp> getEmps() {
    return this.emps;
}

B)@Target注释

或使用@Target注释(如错误消息中所述):

@OneToMany(fetch=FetchType.LAZY, mappedBy="dept")
@Target(Emp.class)
public Set getEmps() {
    return this.emps;
}

C)targetEntity属性

@OneToMany(fetch=FetchType.LAZY, mappedBy="dept", targetEntity=Emp.class)
public Set getEmps() {
    return this.emps;
}