Bonsoir ,
Je suis entrain de développer une petite application permettant de tester les mappages avec hibernate mais je me suis planté sur l’ajout d’une nouvelle école qui possède plusieurs etudiants j'ai eu l'exception suivante :
voici les classes que je possède
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 Initial SessionFactory creation failed.org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.model.Ecole.enseignantses[com.model.Enseignants] Exception in thread "main" java.lang.ExceptionInInitializerError at com.util.hibernateutil.<clinit>(hibernateutil.java:19) at com.util.Test.main(Test.java:20) Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.model.Ecole.enseignantses[com.model.Enseignants] at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1068) at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:600) at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541) at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1136) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859) at com.util.hibernateutil.<clinit>(hibernateutil.java:16) ... 1 more
et la classe étudiant est la suivante :
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 package com.model; // Generated 4 déc. 2012 18:54:36 by Hibernate Tools 3.4.0.CR1 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; /** * Ecole generated by hbm2java */ @Entity @Table(name = "ecole", catalog = "accounts_db") public class Ecole implements java.io.Serializable { private Integer id; private String nom; private String adresse; private String nombreSalles; private Set<Evenements> evenementses = new HashSet<Evenements>(0); private Set<Etudiant> etudiants = new HashSet<Etudiant>(0); private Set<Enseignants> enseignantses = new HashSet<Enseignants>(0); public Ecole() { } public Ecole(String nom, String adresse, String nombreSalles) { this.nom = nom; this.adresse = adresse; this.nombreSalles = nombreSalles; } public Ecole(String nom, String adresse, String nombreSalles, Set<Evenements> evenementses, Set<Etudiant> etudiants, Set<Enseignants> enseignantses) { this.nom = nom; this.adresse = adresse; this.nombreSalles = nombreSalles; this.evenementses = evenementses; this.etudiants = etudiants; this.enseignantses = enseignantses; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "nom", nullable = false, length = 45) public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } @Column(name = "adresse", nullable = false, length = 45) public String getAdresse() { return this.adresse; } public void setAdresse(String adresse) { this.adresse = adresse; } @Column(name = "nombreSalles", nullable = false, length = 45) public String getNombreSalles() { return this.nombreSalles; } public void setNombreSalles(String nombreSalles) { this.nombreSalles = nombreSalles; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "ecole") public Set<Evenements> getEvenementses() { return this.evenementses; } public void setEvenementses(Set<Evenements> evenementses) { this.evenementses = evenementses; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "ecole") public Set<Etudiant> getEtudiants() { return this.etudiants; } public void setEtudiants(Set<Etudiant> etudiants) { this.etudiants = etudiants; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "ecole") public Set<Enseignants> getEnseignantses() { return this.enseignantses; } public void setEnseignantses(Set<Enseignants> enseignantses) { this.enseignantses = enseignantses; } }
et la classe test pour tester l'ajout :
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 package com.model; // Generated 4 déc. 2012 18:54:36 by Hibernate Tools 3.4.0.CR1 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; /** * Etudiant generated by hbm2java */ @Entity @Table(name = "etudiant", catalog = "accounts_db") public class Etudiant implements java.io.Serializable { private Integer id; private Ecole ecole; private String nom; public Etudiant() { } public Etudiant(Ecole ecole, String nom) { this.ecole = ecole; this.nom = nom; } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ecole_FKK", nullable = false) public Ecole getEcole() { return this.ecole; } public void setEcole(Ecole ecole) { this.ecole = ecole; } @Column(name = "nom", nullable = false, length = 45) public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } }
J'aimerais bien savoir s'il vous plait ou ça se plante et comment résoudre ça?
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 package com.util; import java.util.ArrayList; import java.util.HashSet; import org.hibernate.Session; import org.hibernate.Transaction; import com.model.Ecole; import com.model.Etudiant; public class Test { public static void main(String[] args){ Session session; Transaction tx=null; try{ session=hibernateutil.currentSession(); tx=session.beginTransaction(); Ecole ec=new Ecole(); ec.setNom("ESPRIT"); ec.setNombreSalles("34"); Etudiant etd1=new Etudiant(); etd1.setNom("ALLANI"); ec.setEtudiants(new HashSet<Etudiant>()); ec.getEtudiants().add(etd1); session.save(ec); session.flush(); tx.commit(); }catch(Exception e){ e.printStackTrace(); } } }
Merci à vous !
Partager