Il modélise une association ternaire. Pour l'utiliser, vous avez besoin d'une carte. JPA 1.0 ne supporte pas la carte, donc si vous voulez utiliser, vous avez besoin d'Hibernate parce que son support de carte. C'est quelque chose comme
@Entity
public class A {
@ManyToMany
@org.hibernate.annotations.MapKeyManyToMany(
[email protected](name="B_ID")
)
@JoinTable(
name="D",
[email protected](name="A_ID"),
[email protected](name="C_ID")
)
private Map<B, C> bAndC = new HashMap<B, C>();
}
Notez que chaque clé est un B et que chaque valeur est une entité C. En pratique, il lie A et C à travers une entité B.
@Entity
public class B {
@Id
private Integer id;
}
@Entity
public class C {
@Id
private Integer id;
}
Ou si vous ne voulez pas une carte, vous pouvez modéliser une entité ABC selon
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
}
}
}
salutations,
Merci. Très descriptif et utile – soontobeared
Bonne question. Je vous en prie –