方法一:主鍵類用@Embeddable,pojo類仍然用@Entity但是引用主鍵類的對象用@Id
主鍵pojo類:
@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類:
@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即可
方法三:@Id @IdClass(*) 主鍵pojo類無需加注解,原pojo類的id,name屬性保留不變,也無需新增“ComposeIDPK”屬性。 隻在id,name的get方法前都加@Id,并在原pojo類前加 @IdClass(*)
如下:
@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;
作者:習慣沉澱