我有2个分类用户和底部,用户与Bottom有很多关系。 用户有2个复合键
用户id
class TireId {
private String uId;
private String schoolId
}
用户实体:
@IdClass(UserId.class)
@Entity
Class User {
@Id
private String uId;
@Id
private String schoolId
@OneToMany(mappedBy="user")
private List<Bottom> bottoms;
}
BottomId:
class BottomId {
private String bId;
private String cId;
private String schoolId;
}
底层课程:
@IdClass(BottomId.class)
@Entity
class Bottom {
private String bId;
private String cId;
private String schoolId;
@ManyToOne
private User user;
}
在上面的例子中,hibernate将在底部表名user_uId和user_schoolId中创建2个附加列。
但我需要User with OneToMany与以下关系的关系
schoolId of user == schoolId of Bottom
and uId of user == cId of of Bottom
我如何存档?
答案 0 :(得分:0)
这是“派生身份”,因此BottomId
应如下所示:
class BottomId {
private String bId;
private UserId user; // matches name of attribute and type of User PK
}
和Bottom
应该像这样映射:
@IdClass(BottomId.class)
@Entity
class Bottom {
@Id
private String bId;
@Id
@JoinColumns({
@JoinColumn(name="cId", referencedColumnName="uId"),
@JoinColumn(name="schoolId", referencedColumnName="schoolId")
})
@ManyToOne
private User user;
}
在第2.4.1节的JPA 2.1规范中讨论了衍生身份(带有示例)。