0

在创建 RESTFul Web 服务时,我有 NetBeans 7.4 生成的实体类代码。当我测试 web 服务时,我得到以下异常

 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.

企业实体

@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;
    }

休眠.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>

休眠.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>
4

1 回答 1

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;
}
于 2013-11-15T08:24:47.190 回答