Bonsoir tout le monde,
je suis débutante dans la programmation et mon petit programme a comme tache d'insérer deux objets (fille et module)de classes différentes dans la base de données chacun dans sa table sachant que l'objet fille a comme attribut une collection de l'objet module(HashSet),relation un à plusieurs.alors j'ai écrit les fichiers du modèle de mappage et tout le reste.Quand j’exécute le programme il ne m'affiche aucune erreur ce pendant aucune insertion n'a lieu .voila les classes et fichiers de mappage
classe Fille:
classe module:
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 package modele; import java.util.HashSet; import java.util.Set; public class Fille implements java.io.Serializable { private Integer numero; private String nom; private String prenom; private Set<Module> Mod = new HashSet<Module>(0); public Fille() { } public Fille(String nom, String prenom,Set Mod) { this.nom = nom; this.prenom = prenom; this.Mod=Mod; } public Integer getNumero() { return this.numero; } public void setNumero(Integer numero) { this.numero = numero; } public String getNom() { return this.nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return this.prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public Set getMod() { return Mod; } public void setMod(Set Mod) { this.Mod = Mod; } }
Fille.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
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 package modele; public class Module implements java.io.Serializable { private Integer numero; private Integer numero_fille; private String designation; private Integer coefficient; public Module(String designation, Integer coefficient) { //this.numero_fille = numero_fille; this.designation = designation; this.coefficient = coefficient; } public Module() { } public Integer getNumero_fille() { return numero_fille; } public void setNumero_fille(Integer numero_fille) { this.numero_fille = numero_fille; } public String getDesignation() { return designation; } public Integer getCoefficient() { return coefficient; } public void setDesignation(String designation) { this.designation = designation; } public void setCoefficient(Integer coefficient) { this.coefficient = coefficient; } public Integer getNumero() { return numero; } }
Module.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
17
18
19
20
21
22
23
24
25 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated 25 avr. 2020 23:01:50 by Hibernate Tools 4.3.1 --> <hibernate-mapping> <class name="modele.Fille" table="fille" catalog="famil" optimistic-lock="version"> <id name="numero" type="java.lang.Integer"> <column name="numero" /> <generator class="identity" /> </id> <property name="nom" type="string"> <column name="nom" length="15" not-null="true" /> </property> <property name="prenom" type="string"> <column name="prenom" length="15" not-null="true" /> </property> <set name="Mod" table="module" inverse="true" lazy="true" fetch="select"> <key> <column name="numero" not-null="true"/> </key> <one-to-many class="modele.Module"/> </set> </class> </hibernate-mapping>
La classe contrôleur pour insérer mes objets:
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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated 25 avr. 2020 23:01:50 by Hibernate Tools 4.3.1 --> <hibernate-mapping> <class name="modele.Module" table="module" catalog="famil" optimistic-lock="version"> <id name="numero" type="java.lang.Integer"> <column name="numero" /> <generator class="identity" /> </id> <many-to-one name = "fille" class="modele.Fille" fetch="select"> <column name = "numero_fille" not-null="true"/> </many-to-one> <property name="designation" type="string"> <column name="designation" length="15" not-null="true" /> </property> <property name="coefficient" type="java.lang.Integer"> <column name="coefficient" not-null="true" /> </property> </class> </hibernate-mapping>
un énorme merci à vous
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 package dao; import modele.Fille; import modele.Module; import org.hibernate.Session; import org.hibernate.Transaction; public class filleDao { static Session session = null; static Session sessions = null; public static void insert(Fille f){ try{ session=HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.save(f); tx.commit(); session.close(); } catch(Exception ex){ System.out.println(ex); } } public static void insertt(Module m){ try{ sessions=HibernateUtil.getSessionFactory().openSession(); Transaction txx = sessions.beginTransaction(); sessions.save(m); txx.commit(); sessions.close(); } catch(Exception e){ System.out.println(e); } } public static void main(String[]args){ Fille f = new Fille(); f.setNom("mouni"); f.setPrenom("hhhhh"); System.out.println("ademmmmmmmmm"); insert(f); Module m = new Module(); m.setCoefficient(5); m.setDesignation("bbbbb"); m.setNumero_fille(1); insertt(m); } }![]()
Partager