Bonjour,
Je n'arrive pas à me sortir d'un petit problème, ainsi je me tourne vers vous.
Je travaille sur un projet qui gère un championnat d'un sport collectif. Je travaille sur Hibernate 3 qui utilise une base MySQL 5.
J'ai à disposition deux classes : Equipe et Personne.
Voici leur description SQL :
La table Personne a une clé étrangère (OID_EQUIPE), correspondant à la clé de la table Equipe.
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 DESC PERSONNE; +----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | OID | int(11) | NO | PRI | NULL | | | NOM | varchar(40) | NO | | NULL | | | PRENOM | varchar(40) | YES | | NULL | | | DATE_NAISSANCE | date | NO | | NULL | | | LICENCE | int(11) | YES | | NULL | | | ESTARBITRE | tinyint(1) | YES | | 0 | | | ESTJOUEUR | tinyint(1) | YES | | 1 | | | ESTPRESIDENT | tinyint(1) | YES | | 0 | | | OID_EQUIPE | int(11) | YES | MUL | NULL | | +----------------+-------------+------+-----+---------+-------+ DESC EQUIPE; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | OID | int(11) | NO | PRI | NULL | | | NOM | varchar(50) | NO | UNI | NULL | | +-------+-------------+------+-----+---------+-------+
Côté Hibernate, suivent les fichiers nécessaires dont j'omets volontairement les déclarations de DTD :
(1) hibernate.cfg.xml
(2) Personne.hbm.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/championnat</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">justdoit</property> <property name="hibernate.show_sql">true</property> <!-- Mapping --> <mapping resource="champ/Personne.hbm.xml"/> <mapping resource="champ/Equipe.hbm.xml"/> </session-factory> </hibernate-configuration>
(3) Equipe.hbm.xml
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 <hibernate-mapping> <class name="champ.Personne" table="PERSONNE"> <id name="OID" column="OID"> <generator class="increment"/> </id> <property name="nom"/> <property name="prenom"/> <property name="dateNaissance" type="date" column="DATE_NAISSANCE"/> <property name="licence"/> <property name="arbitre" column="ESTARBITRE"/> <property name="joueur" column="ESTJOUEUR"/> <property name="president" column="ESTPRESIDENT"/> <many-to-one name="equipe" column="OID_EQUIPE"/> </class> </hibernate-mapping>
Maintenant, les classes mappées côté Java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <hibernate-mapping> <class name="champ.Equipe" table="EQUIPE"> <id name="OID" column="OID"> <generator class="increment"/> </id> <property name="nom"/> <set name="joueurs" inverse="true"> <key column="OID_EQUIPE"/> <one-to-many class="champ.Personne"/> </set> </class> </hibernate-mapping>
(1) Personne.java
(2) Equipe.java
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 package champ; import java.sql.Date; public class Personne { private Long OID; private String nom; private String prenom; private Date dateNaissance; private int licence; private boolean arbitre; private boolean joueur; private boolean president; private Equipe equipe; public Long getOID() { return OID; } public void setOID(Long oid) { OID = oid; } 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 Date getDateNaissance() { return dateNaissance; } public void setDateNaissance(Date dateNaissance) { this.dateNaissance = dateNaissance; } public int getLicence() { return licence; } public void setLicence(int licence) { this.licence = licence; } public boolean isArbitre() { return arbitre; } public void setArbitre(boolean arbitre) { this.arbitre = arbitre; } public boolean isJoueur() { return joueur; } public void setJoueur(boolean joueur) { this.joueur = joueur; } public boolean isPresident() { return president; } public void setPresident(boolean president) { this.president = president; } public Equipe getEquipe() { return equipe; } public void setEquipe(Equipe equipe) { this.equipe = equipe; } }
Mon problème est le suivant : j'ai une bonne dizaine d'instances de Personne dont le champ OID_EQUIPE est commun. Malheureusement, quand j'exécute mon code Java, seule la première instance est remontée, je ne vois pas pourquoi.
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 package champ; import java.util.Set; public class Equipe { private Long OID; private String nom; private Set joueurs; public Long getOID() { return OID; } public void setOID(Long oid) { OID = oid; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public Set getJoueurs() { return joueurs; } public void setJoueurs(Set joueurs) { this.joueurs = joueurs; } }
Voici le code Java qui ne fonctionne pas tout à fait :
La méthode chercherEquipe fait correctement son travail, elle remonte la bonne instance d'Equipe. Malheureusement, la méthode joueursComposantEquipe ne remonte que la première instance Personne, bien que Equipe ait pour attribut joueurs de type Set. L'erreur serait-elle là ? Ou alors, mon code Java est-il complètement faux ? A priori, je suis supposé pouvoir obtenir toute la liste des joueurs sans passer par la classe Criteria.
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 private static Equipe chercherEquipe(Long OID) { Equipe uneEquipe = null; try { uneEquipe = (Equipe)session.get(Equipe.class, OID); /*java.util.List<Equipe> list = session.createCriteria(Equipe.class).add(Restrictions.eq("OID", OID)).list(); if (list != null) { Iterator<Equipe> i = list.iterator(); while (i.hasNext()) { uneEquipe = i.next(); } }*/ } catch (Exception e) { System.out.println(e.toString()); } return uneEquipe; } private static void joueursComposantEquipe(Long OID_equipe) { Equipe E = chercherEquipe(OID_equipe); if (E == null) return; try { System.out.println("--------------------"); System.out.println(E.toString()); System.out.println("--------------------"); System.out.println(E.getJoueurs().size()); } catch (Exception e) { System.out.println("--- joueursComposantEquipe ---"); e.printStackTrace(); } }
A l'exécution de la méthode joueursComposantEquipe, le log indique :
La clause where me laisse pourtant croire qu'elle impose une valeur stricte sur l'attribut OID_EQUIPE...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Hibernate: select joueurs0_.OID_EQUIPE as OID9_1_, joueurs0_.OID as OID1_, joueurs0_.OID as OID0_, joueurs0_.nom as nom0_0_, joueurs0_.prenom as prenom0_0_, joueurs0_.DATE_NAISSANCE as DATE4_0_0_, joueurs0_.licence as licence0_0_, joueurs0_.ESTARBITRE as ESTARBITRE0_0_, joueurs0_.ESTJOUEUR as ESTJOUEUR0_0_, joueurs0_.ESTPRESIDENT as ESTPRESI8_0_0_, joueurs0_.OID_EQUIPE as OID9_0_0_ from PERSONNE joueurs0_ where joueurs0_.OID_EQUIPE=?
Merci par avance de l'aide que vous pourrez m'apporter.
Partager