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
精彩评论