hibernate联合主键 注解方式

上一篇博客写了如何用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类的idname属性保留不变,也无需新增“ComposeIDPK”属性。 只在idnameget方法前都加@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!对象

相关文章
相关标签/搜索