Hibernate注释(金字塔结构 - 同表)

时间:2011-01-17 07:39:28

标签: java hibernate annotations


我有一个包含以下字段的人员表:

id,name,parent_id。

parent_id实际上是列ID的FK。 数据应该像这样(像金字塔一样):

"id"   "name"                  "parent_id"

"1"    "I am the Top Father"   "1"
"2"    "My Father Is 1"        "1"
"3"    "My Father Is 2"        "2"
"4"    "My Father Is 2"        "2"

我的豆子看起来怎么样?

由于

1 个答案:

答案 0 :(得分:4)

@Entity
@Table(name="persons")
public class Person {

    @Id
    private Long id;
    private String name;

    @ManyToOne
    @JoinColumn(name="parent_id")
    private Person parent;

    ...

}

查看Hibernate Annotations参考。