【发布时间】:2016-11-03 15:15:14
【问题描述】:
我知道问题可能很简单,但是,无论如何,我想在一张表中使用 hibernate 实现简单的父子关系:
Parent
| \
Child1 Child2
| |
Junior1 Junior2
所以它在数据库中应该如下所示:
id | name | parent_id
1 Parent null
2 Child1 1
3 Child2 1
4 Junior1 2
5 Junior2 3
如果是@Entity 类:
@Entity
@Table(name = "PARENT_CHILD")
public class ParentChild {
@Id
@Column(name = "ID", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "NAME", nullable = false)
private String name;
private String parentId; // <------ how this should be mapped?
}
关于我应该如何映射 parentId 或以另一种方式进行以下操作的最佳做法是什么?谢谢
【问题讨论】:
标签: java hibernate jpa orm persistence