为给定关系创建实体一对一关系,一个表的主键是另一个表的外键

时间:2016-07-28 09:02:05

标签: hibernate

hierarchy

发送给我们使用HIBERNATE JPA注释创建这种关系的实体

1 个答案:

答案 0 :(得分:0)

这是用户和他的个人资料之间的OneToOne映射

@Entity
public class User {

@Id
private int id;
private String name;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
private Profile profile;

}

个人资料实体

@Entity
public class Profil{

    @GenericGenerator(name = "generator", strategy = "foreign", 
    parameters = @Parameter(name = "property", value = "user"))
    @Id
    @GeneratedValue(generator = "generator")
    private int id;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private User user;

}