Bonjour à tous,
Je suis en train de développer une petite application et je me heurte à un petit problème que je n'arrive pas à résoudre seul, voila donc pourquoi je viens vers vous.
Je développe donc une application avec des entités JPA d'un coté et des WebServices REST de l'autre coté qui exposent du JSON comme ressources. J'utilise Jackson comme provider JSON c'est pour cela que je n'ai pas d'annotations JAXB. J'utilise les annotations Jackson.
J'ai deux classes:
Classe Salle:
et une classe Eleve:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.test; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import java.io.Serializable; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; @Entity public class Salle implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String nom; @ManyToMany @JoinTable(name = "Salle_Eleve", joinColumns = {@JoinColumn(name="salleId")}, inverseJoinColumns = {@JoinColumn(name = "eleveId")}) private List<Eleve> eleves; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public List<Eleve> getEleves() { return eleves; } public void setEleves(List<Eleve> eleves) { this.eleves = eleves; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Salle)) { return false; } Salle other = (Salle) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "Salle[ id=" + id + " ]"; } }
Mon problème est que j'ai des dépendances cycliques entre Eleve et Salle. J'aurais pu annoter mon champ "eleves" de ma classe Salle avec une annotation "@JsonIgnore", mais le problème est quand je voudrai enregistrer une nouvelle salle avec des élèves je serais obligé de faire deux appels au serveur une fois pour enregistrer ma salle et une autre fois pour enregistrer la liste des élèves. Si par exemple j'avais une contrainte qui disait que je ne doit pas avoir de salle vide (sans élèves), alors j'aurais un état inconsistant en base.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.test; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import java.io.Serializable; import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Eleve implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String nom; private String prenom; @ManyToMany @JoinTable(name = "Salle_Eleve", joinColumns = {@JoinColumn(name="eleveId")}, inverseJoinColumns = {@JoinColumn(name = "salleId")}) private List<Salle> salles; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public List<Salle> getSalles() { return salles; } public void setSalles(List<Salle> salles) { this.salles = salles; } @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Eleve)) { return false; } Eleve other = (Eleve) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "Eleve[ id=" + id + " ]"; } }
Si vous avez des idées je suis preneur.
Merci à tous.
Partager