Bonjour,
J'ai créé deux tables Joueur et Equipe tel que l'équipe est une clé étrangère dans la classe joueur comme ceci :
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 package com.java.model; // Generated 22 oct. 2016 22:46:31 by Hibernate Tools 4.0.0 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonManagedReference; import org.codehaus.jackson.annotate.JsonProperty; /** * Joueur generated by hbm2java */ @Entity @Table(name = "joueur", catalog = "football") public class Joueur implements java.io.Serializable { private Integer idjoueurs; private Equipe equipe; private String nom; private String prenom; public Joueur() { } public Joueur(Equipe equipe) { this.equipe = equipe; } public Joueur(Equipe equipe, String nom, String prenom) { this.equipe = equipe; this.nom = nom; this.prenom = prenom; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "Idjoueurs", unique = true, nullable = false) public Integer getIdjoueurs() { return this.idjoueurs; } public void setIdjoueurs(Integer idjoueurs) { this.idjoueurs = idjoueurs; } @JsonIgnore @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "idquipe", nullable = false) public Equipe getEquipe() { return this.equipe; } public void setEquipe(Equipe equipe) { this.equipe = equipe; } @Column(name = "nom", length = 45) public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } @Column(name = "prenom", length = 45) public String getPrenom() { return this.prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } }Ensuite j'ai créé une méthode qui permet de me retourner le joueur avec son équipe et j'ai utilisé une architecture REST pour afficher le résultat sous format JSON dans le web comme ceci :
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 package com.java.model; // Generated 22 oct. 2016 22:46:31 by Hibernate Tools 4.0.0 import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonManagedReference; /** * Equipe generated by hbm2java */ @Entity @Table(name = "equipe", catalog = "football") public class Equipe implements java.io.Serializable { private Integer idquipe; private String nomequipe; private Set<Joueur> joueurs = new HashSet<Joueur>(0); public Equipe() { } public Equipe(String nomequipe) { this.nomequipe = nomequipe; } public Equipe(String nomequipe, Set<Joueur> joueurs) { this.nomequipe = nomequipe; this.joueurs = joueurs; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "idquipe", unique = true, nullable = false) public Integer getIdquipe() { return this.idquipe; } public void setIdquipe(Integer idquipe) { this.idquipe = idquipe; } @Column(name = "nomequipe", nullable = false, length = 45) public String getNomequipe() { return this.nomequipe; } public void setNomequipe(String nomequipe) { this.nomequipe = nomequipe; } @JsonIgnore @OneToMany(fetch = FetchType.EAGER, mappedBy = "equipe") public Set<Joueur> getJoueurs() { return this.joueurs; } public void setJoueurs(Set<Joueur> joueurs) { this.joueurs = joueurs; } }
Mon pom.xml est le suivant :
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 @GET @Path("/{id}") @Produces(MediaType.APPLICATION_JSON) public Joueur retournerPersonne(@PathParam("id") int id) { Joueur player = new Joueur(); try{ String[] configArray = new String[] { "ApplicationContext.xml","hibernate.cfg.xml"}; ApplicationContext appContext = new ClassPathXmlApplicationContext(configArray); daoJ = (JoueurIdao) appContext.getBean("daoJJ"); player = (Joueur) daoJ.getInformationObject(id,Joueur.class); Hibernate.initialize(player.getEquipe()); }catch(Exception e ){ logger.error("Exceptions happen!", e); e.printStackTrace(); } return player; }
Le problème est que dans l'affichage je n'obtiens pas l'équipe du joueur.
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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.foot</groupId> <artifactId>football</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>football Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.4.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.1.8.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>football</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
J'obtiens que son id, nom et prénom mais pas la clé étrangère équipe.
Y-a-t-il une idée pour remédier à cette problématique ?
Merci
Partager