3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以制作一個(gè)Embedded class,其中包含兩個(gè)鍵,然后像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 **/
}
完成此任務(wù)的另一種方法是使用@IdClass批注,然后將兩者都id放在該批注中IdClass。現(xiàn)在您可以@Id在兩個(gè)屬性上使用普通注釋
@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;
}
添加回答
舉報(bào)