bonjour tt le monde,
j'ai un pb avec Hibernate dont la trace de l'erreur est la suivante:
mes classes ce sont dant le meme package :0 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.0.5
15 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
15 [main] INFO org.hibernate.cfg.Environment - using CGLIB reflection optimizer
15 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
78 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: com/jmd/library/Clients.hbm.xml
484 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: Clients -> clients
500 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: com/jmd/library/Commandes.hbm.xml
547 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: Commandes -> commandes
609 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: com/jmd/library/Produits.hbm.xml
640 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: Produits -> produits
640 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: com/jmd/library/DetailCommandes.hbm.xml
672 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: DetailCommandes -> detailcommandes
672 [main] INFO org.hibernate.cfg.Configuration - processing extends queue
672 [main] INFO org.hibernate.cfg.Configuration - processing collection mappings
Exception in thread "main" org.hibernate.MappingException: Association references unmapped class: com.jmd.library.Commandes
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2036)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2497)
at org.hibernate.cfg.HbmBinder$SecondPass.doSecondPass(HbmBinder.java:2468)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:884)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
at com.jmd.library.HibernateTest.main(HibernateTest.java:32)
la classe Clients :
et la classe Commandes:
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 package com.jmd.library; import java.util.HashSet; import java.util.Set; public class Clients implements java.io.Serializable { // Fields private static final long serialVersionUID = 1L; private String cin; private String nom; private String prenom; private String adresse; private Set commandeses = new HashSet(0); // Constructors /** default constructor */ public Clients() { super(); } /** full constructor */ public Clients(String nom, String prenom, String adresse, Set commandeses) { this.nom = nom; this.prenom = prenom; this.adresse = adresse; this.commandeses = commandeses; } // Property accessors public Clients(String string, String string2, String string3) { } public String getCin() { return this.cin; } public void setCin(String cin) { this.cin = cin; } 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 String getAdresse() { return this.adresse; } public void setAdresse(String adresse) { this.adresse = adresse; } public Set getCommandeses() { return this.commandeses; } public void setCommandeses(Set commandeses) { this.commandeses = commandeses; } }
et la classe Produits :
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 com.jmd.library; import java.util.Date; import java.util.HashSet; import java.util.Set; public class Commandes implements java.io.Serializable { private static final long serialVersionUID = 1L; // Fields private Integer numcom; private Clients clients; private Date date; private Set detailcommandeses = new HashSet(0); // Constructors /** default constructor */ public Commandes() { super(); } /** full constructor */ public Commandes(Clients clients, Date date, Set detailcommandeses) { this.clients = clients; this.date = date; this.detailcommandeses = detailcommandeses; } // Property accessors public Commandes(String string, Date date2) { } public Integer getNumcom() { return this.numcom; } public void setNumcom(Integer numcom) { this.numcom = numcom; } public Clients getClients() { return this.clients; } public void setClients(Clients clients) { this.clients = clients; } public Date getDate() { return this.date; } public void setDate(Date date) { this.date = date; } public Set getDetailcommandeses() { return this.detailcommandeses; } public void setDetailcommandeses(Set detailcommandeses) { this.detailcommandeses = detailcommandeses; } }
et la classe DetailCommandes :
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 com.jmd.library; import java.util.HashSet; import java.util.Set; public class Produits implements java.io.Serializable { // Fields private static final long serialVersionUID = 1L; private Integer reference; private String libelle; private Long prix; private Set detailcommandeses = new HashSet(0); // Constructors /** default constructor */ public Produits() { super(); } /** full constructor */ public Produits(String libelle, Long prix, Set detailcommandeses) { this.libelle = libelle; this.prix = prix; this.detailcommandeses = detailcommandeses; } // Property accessors public Produits(String string, String string2) { } public Integer getReference() { return this.reference; } public void setReference(Integer reference) { this.reference = reference; } public String getLibelle() { return this.libelle; } public void setLibelle(String libelle) { this.libelle = libelle; } public Long getPrix() { return this.prix; } public void setPrix(Long prix) { this.prix = prix; } public Set getDetailcommandeses() { return this.detailcommandeses; } public void setDetailcommandeses(Set detailcommandeses) { this.detailcommandeses = detailcommandeses; } }
et les fichiers mapping placées dant le meme package :
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 package com.jmd.library; public class DetailCommandes implements java.io.Serializable { // Fields private static final long serialVersionUID = 1L; private String id; private Produits produits; private Commandes commandes; private Integer qte; // Constructors /** default constructor */ public DetailCommandes() { super(); } /** minimal constructor */ public DetailCommandes(Produits produits, Commandes commandes) { this.produits = produits; this.commandes = commandes; } /** full constructor */ public DetailCommandes(Produits produits, Commandes commandes, Integer qte) { this.produits = produits; this.commandes = commandes; this.qte = qte; } // Property accessors public DetailCommandes(String string, String string2, String string3) { } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public Produits getProduits() { return this.produits; } public void setProduits(Produits produits) { this.produits = produits; } public Commandes getCommandes() { return this.commandes; } public void setCommandes(Commandes commandes) { this.commandes = commandes; } public Integer getQte() { return this.qte; } public void setQte(Integer qte) { this.qte = qte; } }
Clients.hbm.xml :
Code xml : 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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Clients" table="clients" schema="dbo" catalog="DBbase1"> <id name="cin" type="java.lang.String"> <column name="cin" length="10" /> <generator class="sequence"></generator> </id> <property name="nom" type="java.lang.String"> <column name="nom" /> </property> <property name="prenom" type="java.lang.String"> <column name="prenom" /> </property> <property name="adresse" type="java.lang.String"> <column name="adresse" /> </property> <set name="commandeses" inverse="true"> <key> <column name="cin" length="10" /> </key> <one-to-many class="com.jmd.library.Commandes" /> </set> </class> </hibernate-mapping>
et Commandes.hbm.xml :
Code xml : 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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Commandes" table="commandes" schema="dbo" catalog="DBbase1"> <id name="numcom" type="java.lang.Integer"> <column name="numcom" /> <generator class="sequence"></generator> </id> <many-to-one name="clients" class="Clients" fetch="select"> <column name="cin" length="10" /> </many-to-one> <property name="date" type="java.util.Date"> <column name="date" length="23" /> </property> <set name="detailcommandeses" inverse="true"> <key> <column name="numcom" not-null="true" /> </key> <one-to-many class="com.jmd.library.Detailcommandes" /> </set> </class> </hibernate-mapping>
et Produits.hbm.xml :
Code xml : 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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Produits" table="produits" schema="dbo" catalog="DBbase1"> <id name="reference" type="java.lang.Integer"> <column name="reference" /> <generator class="sequence"></generator> </id> <property name="libelle" type="java.lang.String"> <column name="libelle" /> </property> <property name="prix" type="java.lang.Long"> <column name="prix" precision="18" scale="0" /> </property> <set name="detailcommandeses" inverse="true"> <key> <column name="reference" not-null="true" /> </key> <one-to-many class="com.jmd.library.Detailcommandes" /> </set> </class> </hibernate-mapping>
et DetailCommandes.hbm.xml :
Code xml : 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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="DetailCommandes" table="detailcommandes" schema="dbo" catalog="DBbase1"> <id name="id" type="java.lang.String"> <column name="id" length="10" /> <generator class="sequence"></generator> </id> <many-to-one name="produits" class="Produits" fetch="select"> <column name="reference" not-null="true" /> </many-to-one> <many-to-one name="commandes" class="Commandes" fetch="select"> <column name="numcom" not-null="true" /> </many-to-one> <property name="qte" type="java.lang.Integer"> <column name="qte" /> </property> </class> </hibernate-mapping>
et le fichier de HibernateTest.java placé dans le meme package :
et le fichier HibernateSessionFactory.java dans le package par defaut 1 autre :
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.jmd.library; import java.util.Date; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; public class HibernateTest { public static void main(String args[]) throws Exception { Configuration config = new Configuration(); config.addClass(Clients.class); config.addClass(Commandes.class); config.addClass(Produits.class); config.addClass(DetailCommandes.class); config.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); config.setProperty("myeclipse.connection.profile", "BaseCommandes"); config.setProperty("hibernate.connection.url", "jdbc:microsoft:sqlserver://localhost:1433"); config.setProperty("hibernate.connection.username", "khalil"); config.setProperty("hibernate.connection.password", "password"); config.setProperty("hibernate.connection.driver_class", "com.microsoft.jdbc.sqlserver.SQLServerDriver"); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Clients clients = new Clients("nom", "prenom", "adresse"); session.save(clients); session.flush() ; tx.commit(); //session.connection(); tx = session.beginTransaction(); Produits produits = new Produits("prix", "libelle"); session.save(produits); session.flush(); tx.commit(); //session.connection(); tx = session.beginTransaction(); Commandes commandes = new Commandes("cin", new Date()); session.save(commandes); session.flush(); tx.commit(); //session.connection(); tx = session.beginTransaction(); DetailCommandes detailcommandes = new DetailCommandes("numcom", "reference", "qte"); session.save(detailcommandes); session.flush(); tx.commit(); //session.connection(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } sessionFactory.close(); } }
et le fichier Hibernate.cfg.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
51
52
53
54
55
56
57 import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; public class HibernateSessionFactory { private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; /** Holds a single instance of Session */ private static final ThreadLocal threadLocal = new ThreadLocal(); /** The single instance of hibernate configuration */ private static final Configuration cfg = new Configuration(); /** The single instance of hibernate SessionFactory */ private static org.hibernate.SessionFactory sessionFactory; public static Session currentSession() throws HibernateException { Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) { if (sessionFactory == null) { try { cfg.configure(CONFIG_FILE_LOCATION); sessionFactory = cfg.buildSessionFactory(); } catch (Exception e) { System.err .println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } session = (sessionFactory != null) ? sessionFactory.openSession() : null; threadLocal.set(session); } return session; } public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } } /** * Default constructor. */ private HibernateSessionFactory() { } }
Code xml : 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 <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">khalil</property> <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="myeclipse.connection.profile">BaseCommandes</property> <property name="connection.password">password</property> <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property> <mapping resource="Clients.hbm.xml"></mapping> <mapping resource="Commandes.hbm.xml"></mapping> <mapping resource="Produits.hbm.xml"></mapping> <mapping resource="DetailCommandes.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
merci d'avance mes amis.
Partager