上一篇博客寫了如何用xml配置聯合主鍵。下面咱們看看如何用annotation配置聯合主鍵java
方法一:主鍵類用@Embeddable,pojo類仍然用@Entity可是引用主鍵類的對象用@Id測試
主鍵pojo類:ui
@Embeddable public class composeIdPK implements Serializable { private String name; private int id; @Column(length=20,name="pkName") public String getName() { return name; } @Column(length=10,name="uuid") public int getId() { return id; } 。。。。。。。。。。。。。。。。。
pojo類:this
@Entity public class composeId { private composeIdPK pk; private int uid; private String title; private String address; @Id public composeIdPK getPk() { return pk; } 。。。。。。。。。。。。。。。。。。
方法二:@EmbeddedlD(*) 主鍵pojo類無需加@EmbeddedlD註解,只需在pojo類新屬性「composeIdPK」的get方法前寫@EmbeddedlD便可spa
方法三:@Id @IdClass(*) 主鍵pojo類無需加註解,原pojo類的id,name屬性保留不變,也無需新增「ComposeIDPK」屬性。 只在id,name的get方法前都加@Id,並在原pojo類前加code
以下:xml
@Entity @IdClass(com.study.model.composeID.composeIdPK.class) public class composeId { //private composeIdPK pk; private int id; private String name; @Id @Column(length=10,name="uuid") public int getId() { return id; } public void setId(int id) { this.id = id; } @Id @Column(length=20,name="pkName") public String getName() { return name; } public void setName(String name) { this.name = name; } private String title; private String address;
測試ok!對象