【问题标题】:create entity for given relation one to one relationship, primary key of one table is foreign key of another table为给定关系创建实体一对一关系,一个表的主键是另一个表的外键
【发布时间】:2016-12-02 13:39:15
【问题描述】:

向我发送 HOE 以使用 HIBERNATE JPA 注释为该关系创建实体

【问题讨论】:

  • 标记为粗鲁或辱骂。请阅读How to Ask 并考虑edit提出您的问题。
  • @xenteros 在这种情况下只需编辑或投票关闭。
  • @ChrisF “给我发个锄头,用 HIBERNATE JPA 注释为这个关系创建实体”绝对是粗鲁的。

标签: hibernate


【解决方案1】:

这是用户与其个人资料之间的 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;

}

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多