Bonjour,
Je développe mon backend avec Spring boot.JIl s'agit d'associer à un utilisateur une liste de role et un role peut appartenir à plusieurs utilisateurs.
Mon soucis est que quand j'appel mon controleur qui est censé me renvoyer la liste de utilisateurs avec leur rôles, le tableau de rôles est vide;

Quand bien même, j'ai bien associer les roles à mes utilisateurs:
Resultat obtenu avec postman sur l'appel localhost:8080/users :
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
 
[
    {
        "id": 1,
        "username": "myUser1",
        "password": "mypass1",
        "email": "jl@mymail1.com",
        "roles": []
    },
    {
        "id": 2,
        "username": "myUser2",
        "password": "mypass2",
        "email": "jl@mymail2.com",
        "roles": []
    }
]
Mon code backend : entité, repository et contrôleurs :
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
 
package com.ln.dao.entities;
 
import java.util.HashSet;
import java.util.Set;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
 
@Entity
@Table(name = "UTILISATEURS")
public class Utilisateur {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String username;
    private String password;
    private String email;
    @ManyToMany(mappedBy = "utilisateurs")
    private Set<Role> roles = new HashSet<Role>();
 
    public Utilisateur() {
        super();
    }
 
    public void addRole(Role role) {
        this.roles.add(role);
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public Set<Role> getRoles() {
        return roles;
    }
 
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
 
}
 
package com.ln.dao.entities;
 
import java.util.HashSet;
import java.util.Set;
 
import javax.persistence.CascadeType;
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.Table;
 
@Entity
@Table(name = "ROLES")
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
 
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "UTILISATEURS_ROLES", joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "UTILISATEUR_ID"))
    private Set<Utilisateur> utilisateurs = new HashSet<Utilisateur>();
 
    public Role(String name) {
        this.name = name;
    }
 
    public void addUtilisateurs(Utilisateur user) {
        this.utilisateurs.add(user);
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "UTILISATEURS_ROLES", joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "UTILISATEUR_ID"))
    public Set<Utilisateur> getUtilisateurs() {
        return utilisateurs;
    }
 
    public void setUtilisateurs(Set<Utilisateur> utilisateurs) {
        this.utilisateurs = utilisateurs;
    }
 
}
 
package com.ln.dao;
 
import org.springframework.data.jpa.repository.JpaRepository;
 
import com.ln.dao.entities.Role;
 
public interface RoleRepository extends JpaRepository<Role, Long> {
 
}
 
package com.ln.dao;
 
import org.springframework.data.jpa.repository.JpaRepository;
 
import com.ln.dao.entities.Utilisateur;
 
public interface UtilisateurRepository extends JpaRepository<Utilisateur, Long> {
 
}
 
package com.ln;
 
 
import java.util.HashSet;
import java.util.Set;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
import com.ln.dao.RoleRepository;
import com.ln.dao.UtilisateurRepository;
import com.ln.dao.entities.Role;
import com.ln.dao.entities.Utilisateur;
 
@SpringBootApplication
public class ContactsApplication implements CommandLineRunner {
 
 
    @Autowired
    UtilisateurRepository utilisateurRepository;
 
    @Autowired
    RoleRepository roleRepository;
 
    public static void main(String[] args) {
        SpringApplication.run(ContactsApplication.class, args);
    }
 
    @Override
    public void run(String... arg0) throws Exception {
 
 
        Utilisateur user1 = new Utilisateur();
        user1.setEmail("jl@mymail1.com");
        user1.setPassword("mypass1");
        user1.setUsername("myUser1");
        Role role1 = new Role("ADMIN");
        roleRepository.save(role1);
        role1.addUtilisateurs(user1);
        Set<Role> userRole1 = new HashSet<Role>();
        userRole1.add(role1);
        user1.addRole(role1);
        utilisateurRepository.save(user1);
 
        Utilisateur user2 = new Utilisateur();
        user2.setEmail("jl@mymail2.com");
        user2.setPassword("mypass2");
        user2.setUsername("myUser2");
        Role role2 = new Role("USER");
        roleRepository.save(role2);
        role2.addUtilisateurs(user2);
        Set<Role> userRole2 = new HashSet<Role>();
        userRole2.add(role2);
        user2.setRoles(userRole2);
        user2.addRole(role2);
        utilisateurRepository.save(user2);
 
    }
 
}
 
package com.ln.web;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.ln.dao.UtilisateurRepository;
import com.ln.dao.entities.Utilisateur;
 
@RestController
@CrossOrigin("*")
public class UtilisateurRestService {
 
    @Autowired
    private UtilisateurRepository utilisateurRepository;
 
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public List<Utilisateur> getUtilisateurs() {
        return utilisateurRepository.findAll();
    }
 
}
Quelqu'un a une idée pour me débloquer afin que je puisse avoir le role d'un utilisateur rempli.