Bonjour,
Dans le cadre d'un projet de fin d'études, je suis en train de préparer ma couche d'accès aux données avec Hibernate. Puisque c'est une 1ere pour moi laors j'ai suivi le tutoriel "Débuter avec Hibernate sous Eclipse" de "Minosis" ave Hibernate Synchronizer.
Dans ce tutoriel, il est fortement conseillé de changer le code généré automatiquement par H.Synchronizer par un code qui est plus conforme à la documentation de Hibernate, c'est ce que j'ai fait..
Je vous poste mon fichier .hbm :
Utilisateur.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
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
117
118
119
120
121
122
123
124 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.oxia.hibernate.Utilisateur" table="utilisateur" > <id name="Id" type="string" > <column name="id_user" sql-type="varchar(20)" /> <generator class="sequence"/> </id> <property name="NomUser" type="string" > <column name="nom_user" sql-type="varchar(20)" not-null="false" /> </property> <property name="PrenomUser" type="string" > <column name="prenom_user" sql-type="varchar(20)" not-null="false" /> </property> <property name="LoginUser" type="string" > <column name="login_user" sql-type="varchar(20)" not-null="false" /> </property> <property name="PaswordUser" type="string" > <column name="pasword_user" sql-type="varchar(20)" not-null="false" /> </property> <property name="MailUser" type="string" > <column name="mail_user" sql-type="longtext" not-null="false" /> </property> <property name="PositionUser" type="string" > <column name="position_user" sql-type="varchar(20)" not-null="false" /> </property> <set name="Produits" table="gere" cascade="all" > <key column="id_user"/> <many-to-many column="id_prod" class="com.oxia.hibernate.Produit"/> </set> <set name="GroupeUsers" table="fait_partie" cascade="all" > <key column="id_user"/> <many-to-many column="id_groupe" class="com.oxia.hibernate.GroupeUser"/> </set> <set name="Artifacts" table="manip_art" cascade="all" > <key column="id_user"/> <many-to-many column="id_art" class="com.oxia.hibernate.Artifact"/> </set> </class> </hibernate-mapping>
Je vous poste aussi le fichier Artifact.hbm.xml
Voici mon fichier de configuration 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
58
59
60
61
62
63
64
65
66
67 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="com.oxia.hibernate.Artifact" table="artifact" > <id name="Id" type="string"> <column name="id_art" sql-type="varchar(10)" /> <generator class="sequence" /> </id> <property name="NomArt" type="string"> <column name="nom_art" sql-type="varchar(10)" not-null="false" /> </property> <property name="verArt" type="string" > <column name="ver_art" sql-type="longtext" not-null="false"/> </property> <property name="StatutArt" column="STATUT_ART" type="string" not-null="false" > <column name="statut_art" sql-type="longtext" not-null="false"/> </property> <property name="DescriptionArt" type="string"> <column name="description_art" sql-type="longtext" not-null="false"/> </property> <many-to-one name="IdProd" column="id_prod" class="com.oxia.hibernate.Produit" not-null="true"> </many-to-one> <set name="DependanceArtsByIdArt" inverse="true" lazy="false" cascade="persist,save-update"> <key column="id_art" /> <one-to-many class="DependanceArt" /> </set> <set name="DependanceArtsByArtIdArt" inverse="true" lazy="false" cascade="persist,save-update"> <key column="id_art" /> <one-to-many class="com.oxia.hibernate.DependanceArt" /> </set> <set name="HistoriqueArts" inverse="true" lazy="false" cascade="persist,save-update"> <key column="id_art" /> <one-to-many class="com.oxia.hibernate.HistoriqueArt" /> </set> <set name="AttachmentArts" table="joint_piece" cascade="all"> <key column="id_art" /> <many-to-many column="id_attach" class="com.oxia.hibernate.AttachmentArt" /> </set> <set name="Utilisateurs" table="manip_art" cascade="all"> <key column="id_art" /> <many-to-many column="id_user" class="com.oxia.hibernate.Utilisateur" /> </set> </class> </hibernate-mapping>
et enfin la classe métier qui traite une insertion simple dans la table Utilisateur :
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 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory > <!-- local connection properties --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/oxiarm</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">xxxx</property> <!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for MySQL --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">false</property> <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <!-- Mapping files --> <mapping resource="com/oxia/hibernate/Utilisateur.hbm.xml" /> <mapping resource="com/oxia/hibernate/Artifact.hbm.xml" /> <mapping resource="com/oxia/hibernate/AttachmentArt.hbm.xml" /> <mapping resource="com/oxia/hibernate/DependanceArt.hbm.xml" /> <mapping resource="com/oxia/hibernate/Design.hbm.xml" /> <mapping resource="com/oxia/hibernate/FaitPartie.hbm.xml" /> <mapping resource="com/oxia/hibernate/Feature.hbm.xml" /> <mapping resource="com/oxia/hibernate/Gere.hbm.xml" /> <mapping resource="com/oxia/hibernate/GroupeUser.hbm.xml" /> <mapping resource="com/oxia/hibernate/HistoriqueArt.hbm.xml" /> <mapping resource="com/oxia/hibernate/Implementation.hbm.xml" /> <mapping resource="com/oxia/hibernate/JointPiece.hbm.xml" /> <mapping resource="com/oxia/hibernate/ManipArt.hbm.xml" /> <mapping resource="com/oxia/hibernate/Produit.hbm.xml" /> <mapping resource="com/oxia/hibernate/Requirement.hbm.xml" /> <mapping resource="com/oxia/hibernate/Testcase.hbm.xml" /> </session-factory> </hibernate-configuration>
TestUtilisateur.java
Voici l'erreur générée dans la console :
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 package com.oxia.service; import java.util.*; import org.hibernate.*; import com.oxia.hibernate.*; import com.oxia.hibernate.dao.UtilisateurDAO; public class TestUtilisateur { public TestUtilisateur() { // TODO Auto-generated constructor stub } public static void main(String args[]) throws HibernateException{ Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Utilisateur user = new Utilisateur(); user.setId("2"); user.setNomUser("ESSAI"); user.setPrenomUser("Karim"); user.setLoginUser("karim"); user.setPaswordUser("karim"); user.setMailUser("test@test.com"); user.setPositionUser("admin"); session.save(user); tx.commit(); HibernateUtil.closeSession(); } }
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 20 mai 2009 17:08:47 org.hibernate.cfg.Environment <clinit> INFO: Hibernate 3.0.5 20 mai 2009 17:08:47 org.hibernate.cfg.Environment <clinit> INFO: hibernate.properties not found 20 mai 2009 17:08:47 org.hibernate.cfg.Environment <clinit> INFO: using CGLIB reflection optimizer 20 mai 2009 17:08:47 org.hibernate.cfg.Environment <clinit> INFO: using JDK 1.4 java.sql.Timestamp handling 20 mai 2009 17:08:47 org.hibernate.cfg.Configuration configure INFO: configuring from resource: /hibernate.cfg.xml 20 mai 2009 17:08:47 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: Configuration resource: /hibernate.cfg.xml 20 mai 2009 17:08:47 org.hibernate.cfg.Configuration addResource INFO: Mapping resource: com/oxia/hibernate/Utilisateur.hbm.xml 20 mai 2009 17:08:48 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues INFO: Mapping class: com.oxia.hibernate.Utilisateur -> utilisateur 20 mai 2009 17:08:48 org.hibernate.cfg.HbmBinder bindCollection INFO: Mapping collection: com.oxia.hibernate.Utilisateur.Produits -> gere 20 mai 2009 17:08:48 org.hibernate.cfg.HbmBinder bindCollection INFO: Mapping collection: com.oxia.hibernate.Utilisateur.GroupeUsers -> fait_partie 20 mai 2009 17:08:48 org.hibernate.cfg.HbmBinder bindCollection INFO: Mapping collection: com.oxia.hibernate.Utilisateur.Artifacts -> manip_art 20 mai 2009 17:08:48 org.hibernate.cfg.Configuration addResource INFO: Mapping resource: com/oxia/hibernate/Artifact.hbm.xml 20 mai 2009 17:08:48 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues INFO: Mapping class: com.oxia.hibernate.Artifact -> artifact 20 mai 2009 17:08:48 org.hibernate.cfg.Configuration add GRAVE: Could not compile the mapping document org.hibernate.MappingException: column attribute may not be used together with <column> subelement at org.hibernate.cfg.HbmBinder.bindColumns(HbmBinder.java:946) at org.hibernate.cfg.HbmBinder.bindColumnsOrFormula(HbmBinder.java:1313) at org.hibernate.cfg.HbmBinder.bindSimpleValue(HbmBinder.java:1001) at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:1769) at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:1728) at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:318) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:236) at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:152) at org.hibernate.cfg.Configuration.add(Configuration.java:362) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:400) at org.hibernate.cfg.Configuration.addResource(Configuration.java:449) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184) at org.hibernate.cfg.Configuration.configure(Configuration.java:1112) at org.hibernate.cfg.Configuration.configure(Configuration.java:1098) at com.oxia.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:13) at com.oxia.service.TestUtilisateur.main(TestUtilisateur.java:16) Exception in thread "main" java.lang.ExceptionInInitializerError at com.oxia.service.TestUtilisateur.main(TestUtilisateur.java:16) Caused by: java.lang.RuntimeException: Problème de configuration : Error reading resource: com/oxia/hibernate/Artifact.hbm.xml at com.oxia.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:15) ... 1 more Caused by: org.hibernate.MappingException: Error reading resource: com/oxia/hibernate/Artifact.hbm.xml at org.hibernate.cfg.Configuration.addResource(Configuration.java:452) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184) at org.hibernate.cfg.Configuration.configure(Configuration.java:1112) at org.hibernate.cfg.Configuration.configure(Configuration.java:1098) at com.oxia.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:13) ... 1 more Caused by: org.hibernate.MappingException: column attribute may not be used together with <column> subelement at org.hibernate.cfg.HbmBinder.bindColumns(HbmBinder.java:946) at org.hibernate.cfg.HbmBinder.bindColumnsOrFormula(HbmBinder.java:1313) at org.hibernate.cfg.HbmBinder.bindSimpleValue(HbmBinder.java:1001) at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:1769) at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:1728) at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:318) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:236) at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:152) at org.hibernate.cfg.Configuration.add(Configuration.java:362) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:400) at org.hibernate.cfg.Configuration.addResource(Configuration.java:449) ... 8 more
Je vous remercie d'avance pour votre aide
Partager