Hibernate:Relation plusieurs-à-plusieurs avec propriétés de jointure
De WIKI.minetti.org
Révision de 7 avril 2016 à 17:15 par Jp (discussion | contributions) (Page créée avec « == But == Lier deux objets '''A''' et '''B''': * Un objet '''A''' est lié à plusieurs objets '''B'''; * Un objet '''B''' est lié à plusieurs objets '''A'''; Chaque as... »)
Sommaire
But
Lier deux objets A et B:
- Un objet A est lié à plusieurs objets B;
- Un objet B est lié à plusieurs objets A;
Chaque association entre un objet A et B comporte des propriétés, appelées propriétés de jointure. Pour cela, il faudra ajouter un objet qui va jouer le rôle de jointure entre A et B.
Au final, on aura donc:
- L'objet A qui contient une liste d'objets Jointure_AB;
- L'objet B qui contient une liste d'objets Jointure_AB;
- L'objet JointureAB qui contient un objet A et un objet B.
Diagramme de classe
TODO
Tables
TODO
Annotations
Relation de la classe A:
@OneToMany(mappedBy = "objA")
@Cascade( { CascadeType.ALL })
private Set<JointureAB> jointureABList = new HashSet<>();
Relation de la classe B:
@OneToMany(mappedBy = "objB")
@Cascade( { CascadeType.ALL })
private Set<JointureAB> jointureABList = new HashSet<>();
Classe JointureAB:
@Table(name = "JOINTURE_AB")
@Entity
public final class JointureAB implements Serializable {
private static final long serialVersionUID = 2151816667859090319L;
@Id
@Column(name="A_ID")
private Integer objAId = null;
@Id
@Column(name="B_ID")
private Integer objBId = null;
@ManyToOne
@PrimaryKeyJoinColumn(name="A_ID", referencedColumnName="ID")
@ForeignKey(name = "FK_JOINTURE_AB_A")
private A objA = null;
@ManyToOne
@PrimaryKeyJoinColumn(name="B_ID", referencedColumnName="ID")
@ForeignKey(name = "FK_JOINTURE_AB_B")
private B objB = null;
@Column(name = "PROPRIETE_1")
private Double property1 = null;
public PersistentExposureParametersImpl() {
super();
}
public PersistentExposureParametersImpl(final A objA, final B objB) {
super();
this.objAId = objA.getId();
this.objBId = objB.getId();
this.objA = objA;
this.objB = objB;
}
protected Integer getObjAId() {
return this.objA.getId();
}
protected Integer getObjBId() {
return this.objB.getId();
}
public A getObjA() {
return this.objA;
}
protected void setObjA(final A objA) {
this.objAId = objA.getId();
this.objA = objA;
}
public A getObjB() {
return this.objB;
}
protected void setObjB(final B objB) {
this.objBId = objB.getId();
this.objB = objB;
}
public Double getProperty1() {
return this.property1;
}
public void setProperty1(final Double property1) {
this.property1 = property1;
}
...
}