3 回答
TA贡献1797条经验 获得超6个赞
您可以制作一个Embedded class,其中包含两个键,然后像EmbeddedId中一样引用该类Entity。
您将需要@EmbeddedId和@Embeddable注释。
@Entity
public class YourEntity {
@EmbeddedId
private MyKey myKey;
@Column(name = "ColumnA")
private String columnA;
/** Your getters and setters **/
}
@Embeddable
public class MyKey implements Serializable {
@Column(name = "Id", nullable = false)
private int id;
@Column(name = "Version", nullable = false)
private int version;
/** getters and setters **/
}
完成此任务的另一种方法是使用@IdClass批注,然后将两者都id放在该批注中IdClass。现在您可以@Id在两个属性上使用普通注释
@Entity
@IdClass(MyKey.class)
public class YourEntity {
@Id
private int id;
@Id
private int version;
}
public class MyKey implements Serializable {
private int id;
private int version;
}
添加回答
举报