复合主键的应用 (EmbeddedId)
主表
@Entity
public class A implements Serializable...{
@EmbeddedId
private AId aId;
public AId getAId() ...{
return aId;
}
public void setAId(AId aId) ...{
this.aId = aId;
}
}
复合主键
@Embeddable
public class AId implements Serializable ...{
@OneToOne
@JoinColumn( name = "bid" )
private B b;
@OneToOne
@JoinColumn( name = "cid" )
private C c;
public B getB() ...{
return b;
}
public void setB(B b) ...{
this.b = b;
}
public C getC() ...{
return c;
}
public void setC(C c) ...{
this.c = c;
}
}
关联表
@Entity
public class B ...{
@Id
@GeneratedValue
private Long id;
public Long getId() ...{
return id;
}
public void setId(Long id) ...{
this.id = id;
}
}
@Entity
public class C ...{
@Id
@GeneratedValue
private Long id;
public Long getId() ...{
return id;
}
public void setId(Long id) ...{
this.id = id;
}
}
复合主键的应用 (IdClass)
主表
@Entity
@IdClass(OrderLinePk.class)
public class OrderLine ...{
@Id
public Order order;
@Id
public Product product;
}
主键类
public class OrderLinePk implements Serializable ...{
@ManyToOne
@JoinColumn(name = "foo", nullable = false)
public Order order;
@ManyToOne
@JoinColumn(name = "bar", nullable = false)
public Product product;
}
关联表
@Entity
public class Product ...{
@Id
public String name;
}
@Entity
@Table(name = "OrderTableFoobar")
public class Order ...{
@Id
@GeneratedValue
public Integer id;
}