คุณสามารถสร้าง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;
}
@IdClassคำอธิบายประกอบคำแนะนำอีกประการหนึ่งที่ฉันพบคือ@Columnคำอธิบายประกอบควรเข้าไปในฟิลด์ของคลาสเอนทิตี (YourEntityในโค้ดตัวอย่างของ RohitJan)