目标实体未定义与OneToMany关系的错误

时间:2015-11-02 11:14:12

标签: java one-to-many javax.persistence

我跟随this tutorial使JPA与EJB3.0一起工作

package example.pojo;

@Entity
public class Cat {

    private String name;
    private Date birthDate;
    private int id;
    private Cat mother;
    private List kittens;

    public Cat() {
        super();
        setKittens(new ArrayList());
    }

    @ManyToOne(optional = true)
    public Cat getMother() {
        return mother;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "mother", targetEntity = example.pojo.Cat)
    public List getKittens() {
        return kittens;
    }

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }
}

我想在同一个实体的两个属性之间建立关系,但这里的映射不起作用。请向我解释我做错了什么,以及如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您必须将该实体放在persistence.xml标签区域的persistence-unit中,例如:

<persistence-unit>
  <class>samplePackage.Cat</class>
</persistence-unit>

添加时,它会被固定