我想实现以下架构:
表A:
a_id
(其他专栏)
表B:
b_id
(其他专栏)
表C:
c_id
(其他专栏)
表D:
a_id_fk
b_id_fk
c_id_fk
我想知道如何创建实体D.表D中的所有键a_id_fk
,b_id_fk
和c_id_fk
形成一个复合主键。
由于
答案 0 :(得分:1)
它模拟三元关联。要使用它,您需要一张地图。 JPA 1.0不支持Map,所以如果你想使用你需要Hibernate,因为它的Map支持。它就像
@Entity
public class A {
@ManyToMany
@org.hibernate.annotations.MapKeyManyToMany(
joinColumns=@JoinColumn(name="B_ID")
)
@JoinTable(
name="D",
joinColumns=@JoinColumn(name="A_ID"),
inverseJoinColumns=@JoinColumn(name="C_ID")
)
private Map<B, C> bAndC = new HashMap<B, C>();
}
请注意,每个键都是B,每个值都是C实体。在实践中,它通过B实体绑定A和C.
@Entity
public class B {
@Id
private Integer id;
}
@Entity
public class C {
@Id
private Integer id;
}
或者,如果您不想要地图,则可以根据
为实体ABC建模public class AbC {
@ManyToOne
@JoinColumn(name="A_ID", insertable=false, updateable=false)
private A a;
@ManyToOne
@JoinColumn(name="B_ID", insertable=false, updateable=false)
private B b;
@ManyToOne
@JoinColumn(name="C_ID", insertable=false, updateable=false)
private C c;
@EmbeddedId
private A_b_C_i_D id;
@Embeddable
public static class A_b_C_i_D implements Serializable {
@Column(name="A_ID", updateable=false)
private Integer a_i_d;
@Column(name="B_ID", updateable=false)
private Integer b_i_d;
@Column(name="C_ID", updateable=false)
private Integer c_i_d;
// getter's and setter's
public boolean equals(Object o) {
if(o == null)
return false;
if(!(o instanceof A_b_C_i_D))
return false;
A_b_C_i_D other = (A_b_C_i_D) o;
if(!(getA_i_d().equals(other.getA_i_d()))
return false;
if(!(getB_i_d().equals(other.getB_i_d()))
return false;
if(!(getC_i_d().equals(other.getC_i_d()))
return false;
return true;
}
public int hashcode() {
// hashcode implementation
}
}
}
的问候,