如何实现Hibernate复合键关系?

时间:2015-10-31 12:50:38

标签: java hibernate

我有看起来像这样的clases。我想将用户与地点联系起来。 City-Locality之间存在OneToMany关系的地方。如何实现这个

.vcenter {
display: inline-block;
vertical-align: middle;
float: none;}

1 个答案:

答案 0 :(得分:0)

您的模型缺乏清晰度 - 用户实体中没有关于城市或地区的信息。但是,如果要向用户实体添加locality id字段,解决方案将如下所示:

@Entity
public class User {
    @Id
    private Long userId;

    private String email;
    private String password;
    private String firstName;
    private String lastName;
    private String phone;

    @OneToOne
    @JoinColumn(name="LOCALITY_ID")
    private Locality locality;

    //setters/getters omitted
}


@Entity
public class City {

    @Id
    private Long id;
    private String cityName;
    private String pinCode;

    @OneToMany
    @JoinColumn(name="LOCALITY_ID") // join column is in table for LOCALITY
    private Set<Locality> localities;

    //setters/getters omitted
}