方法一:主键类用@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;
本文为转载, 源出处未找到..见谅!