Poster un commentaire ou une réponse
Les relations JPA

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION ONE TO ONE bidirectionnelle--------------
diagramme de classe:

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code:
@Entity
public class A implements Serializable {
private Integer idA;
private static final long serialVersionUID = 1L;
private B b;
public A() {
super();
}
@Id
public Integer getIdA() {
return this.idA;
}
public void setIdA(Integer idA) {
this.idA = idA;
}
@OneToOne
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
@Entity
public class B implements Serializable {
private Integer idB;
private static final long serialVersionUID = 1L;
private A a;
public B() {
super();
}
@Id
public Integer getIdB() {
return this.idB;
}
public void setIdB(Integer idB) {
this.idB = idB;
}
@OneToOne(mappedBy = "b")
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
-----------> Résultat dans la base de donnée:
2 tables
A (master): idA + b_IDB (foreign key)
B(slave) : idB

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION ONE TO ONE unidirectionnelle--------------
diagramme de classe:
Publicité

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code:
@Entity
public class D implements Serializable {
private Integer idD;
private static final long serialVersionUID = 1L;
private C c;
public D() {
super();
}
@Id
public Integer getIdD() {
return this.idD;
}
public void setIdD(Integer idD) {
this.idD = idD;
}
@OneToOne
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
}
@Entity
public class C implements Serializable {
private Integer idC;
private static final long serialVersionUID = 1L;
public C() {
super();
}
@Id
public Integer getIdC() {
return this.idC;
}
public void setIdC(Integer idC) {
this.idC = idC;
}
}
-----------> Résultat dans la base de donnée:
2 tables :
D (id + c_Id koreign Key)
C (id)

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION ONE TO MANY bidirectionnelle--------------
Diagramme de clase:

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code:
@Entity
public class E implements Serializable {
private Integer idE;
private static final long serialVersionUID = 1L;
private List<F> fs;
public E() {
super();
}
@Id
public Integer getIdE() {
return this.idE;
}
public void setIdE(Integer idE) {
this.idE = idE;
}
@OneToMany(mappedBy = "e")
public List<F> getFs() {
return fs;
}
public void setFs(List<F> fs) {
this.fs = fs;
}
}
@Entity
public class F implements Serializable {
private Integer idF;
private static final long serialVersionUID = 1L;
private E e;
public F() {
super();
}
@Id
public Integer getIdF() {
return this.idF;
}
public void setIdF(Integer idF) {
this.idF = idF;
}
@ManyToOne
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
}
---------> Résultat dans la base de donnée:
2 tables:
E (slave) : idE
F (master): idF + e_idE

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION ONE TO MANY unidirectionnelle--------------
Diagramme de classes:
Publicité

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code:
@Entity
public class G implements Serializable {
private Integer idG;
private static final long serialVersionUID = 1L;
public G() {
super();
}
@Id
public Integer getIdG() {
return this.idG;
}
public void setIdG(Integer idG) {
this.idG = idG;
}
}
@Entity
public class H implements Serializable {
private Integer idH;
private static final long serialVersionUID = 1L;
private G g;
public H() {
super();
}
@Id
public Integer getIdH() {
return this.idH;
}
public void setIdH(Integer idH) {
this.idH = idH;
}
@ManyToOne
public G getG() {
return g;
}
public void setG(G g) {
this.g = g;
}
}
---------> Résultat dans la base de donnée:
2 tables:
G: IDG
H: IDH + g_IdG

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION ONE TO MANY unidirectionnelle--------------
Diagramme de classes:

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code:
@Entity
public class I implements Serializable {
private Integer idI;
private static final long serialVersionUID = 1L;
private List<J> js;
public I() {
super();
}
@Id
public Integer getIdI() {
return this.idI;
}
public void setIdI(Integer idI) {
this.idI = idI;
}
@OneToMany
public List<J> getJs() {
return js;
}
public void setJs(List<J> js) {
this.js = js;
}
}
@Entity
public class J implements Serializable {
private Integer idJ;
private static final long serialVersionUID = 1L;
public J() {
super();
}
@Id
public Integer getIdJ() {
return this.idJ;
}
public void setIdJ(Integer idJ) {
this.idJ = idJ;
}
}
----------> Résultat dans la base de donnée:
3 tables:
I: idI
J: idJ
I_J: I_idI + Js_idJ

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION MANAY TO MANY bidirectionnelle--------------
Diagramme de classes:
Publicité

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code:
@Entity
public class K implements Serializable {
private Integer idK;
private static final long serialVersionUID = 1L;
private List<L> ls;
public K() {
super();
}
@Id
public Integer getIdK() {
return this.idK;
}
public void setIdK(Integer idK) {
this.idK = idK;
}
@ManyToMany
public List<L> getLs() {
return ls;
}
public void setLs(List<L> ls) {
this.ls = ls;
}
}
@Entity
public class L implements Serializable {
private Integer idL;
private static final long serialVersionUID = 1L;
private List<K> ks;
public L() {
super();
}
@Id
public Integer getIdL() {
return this.idL;
}
public void setIdL(Integer idL) {
this.idL = idL;
}
@ManyToMany(mappedBy = "ls")
public List<K> getKs() {
return ks;
}
public void setKs(List<K> ks) {
this.ks = ks;
}
}
----------> Résultat dans la base de donnée:
3 tables:
K: idK
L: idL
K_L: ks_idK + ls_idL

nb posts:722
nb discussions:131
inscrit le :02-12-2014
-----------RELATION MANAY TO MANY porteuse de données--------------
Pour comprendre cette relation, on va prendre l'exemple d'un employé qui peut participer à plusieurs projets et un projet est réalisé par plusieurs employés.
La classe participation contient la date et le type du projet.
Diagramme de classes:

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Il nous est impossible de traduire directement la relation "many to many porteuse de données".
Pour cela on doit transformer le diagramme de classes comme suit:

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code classe Employee:
package training.manyToManyWithData;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
* Entity implementation class for Entity: Employee
*
*/
@Entity
public class Employee implements Serializable {
private Integer id;
private static final long serialVersionUID = 1L;
private List<Participation> participations;
public Employee() {
super();
}
@Id
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(mappedBy = "employee")
public List<Participation> getParticipations() {
return participations;
}
public void setParticipations(List<Participation> participations) {
this.participations = participations;
}
}

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code classe Project:
package training.manyToManyWithData;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
* Entity implementation class for Entity: Project
*
*/
@Entity
public class Project implements Serializable {
private Integer id;
private static final long serialVersionUID = 1L;
private List<Participation> participations;
public Project() {
super();
}
@Id
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(mappedBy = "project")
public List<Participation> getParticipations() {
return participations;
}
public void setParticipations(List<Participation> participations) {
this.participations = participations;
}
}

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code classe Participation:
package training.manyToManyWithData;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
/**
* Entity implementation class for Entity: Participation
*
*/
@Entity
public class Participation implements Serializable {
private ParticipationId participationId;
private Date dateOfParticipation;
private String type;
private static final long serialVersionUID = 1L;
private Employee employee;
private Project project;
public Participation() {
super();
}
public Date getDateOfParticipation() {
return this.dateOfParticipation;
}
public void setDateOfParticipation(Date dateOfParticipation) {
this.dateOfParticipation = dateOfParticipation;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
@EmbeddedId
public ParticipationId getParticipationId() {
return participationId;
}
public void setParticipationId(ParticipationId participationId) {
this.participationId = participationId;
}
@ManyToOne
@JoinColumn(name = "idEmployee", referencedColumnName = "id", insertable = false, updatable = false)
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@ManyToOne
@JoinColumn(name = "idProject", referencedColumnName = "id", updatable = false, insertable = false)
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
}

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Code classe ParticipationId:
On doit spécifier "@Embeddable" qui signifie que c'est une classe ID.
package training.manyToManyWithData;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class ParticipationId implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idEmployee;
private Integer idProject;
public ParticipationId() {
}
public Integer getIdEmployee() {
return idEmployee;
}
public void setIdEmployee(Integer idEmployee) {
this.idEmployee = idEmployee;
}
public Integer getIdProject() {
return idProject;
}
public void setIdProject(Integer idProject) {
this.idProject = idProject;
}
public ParticipationId(Integer idEmployee, Integer idProject) {
super();
this.idEmployee = idEmployee;
this.idProject = idProject;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((idEmployee == null) ? 0 : idEmployee.hashCode());
result = prime * result
+ ((idProject == null) ? 0 : idProject.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ParticipationId other = (ParticipationId) obj;
if (idEmployee == null) {
if (other.idEmployee != null)
return false;
} else if (!idEmployee.equals(other.idEmployee))
return false;
if (idProject == null) {
if (other.idProject != null)
return false;
} else if (!idProject.equals(other.idProject))
return false;
return true;
}
}

nb posts:722
nb discussions:131
inscrit le :02-12-2014
Pour générer les fonctions "equals" et "hashcode" on fait un clic droit "source" "Generate hashcode() and equals()" comme suit:

nb posts:722
nb discussions:131
inscrit le :02-12-2014

nb posts:722
nb discussions:131
inscrit le :02-12-2014
---------> Résultat dans la base de donnée:
3 tables:
Employee: id
Project: id
Participation: idEmployee,idProject, type, dateOfParticipation
Poster |