1 回答

TA贡献1806条经验 获得超8个赞
这些关系是“派生身份”;因此您的 ID 类应如下所示(请注意外键字段的类型与其相应实体字段的类型不同):
public class ReviewId implements Serializable {
private static final long serialVersionUID = 1L;
private String reviewer; // matches name of @Id attribute and type of User PK
private Long reviewedAlbumId;
// ...
}
public static class VoteId implements Serializable {
private static final long serialVersionUID = 1L;
private String voter; // matches name of @Id attribute and type of User PK
private ReviewId review; // matches name of @Id attribute and type of Review PK
// ...
}
JPA 2.2 规范的第 2.4.1节讨论了派生身份(并附有示例)。
另外,作为旁注,@IdClass
它有点老派,但@EmbeddedId
更干净,消除了实体及其密钥中重复的代码。
添加回答
举报