开发者

How to model hierarchy using JPA

I am looking to build a tree based hierarchy.

@Entity
class Category {

  @I开发者_Python百科d
  private String id;

  @Column
  private String name;

  @ManyToOne
  private Category parent;

}

Should the above declaration be ManyToOne or OneToOne? I am planning to use ManyToOne. Is this correct?


ManyToOne, if you plan to have a tree, as a parent can have more than one children. Look at this example from Hibernate's test suite: https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/test/java/org/hibernate/test/annotations/manytoone/Node.java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜