Bonsoir tout le monde,
Voila, j'ai déjà utilisé plusieurs fois Hibernate, mais pour un projet perso, je souhaite le mettre en place tout seul comme un grand en partant de zéro. J'ai pour l'instant une configuration minimaliste (pas de spring, pas de maven, pas de plugin Hibernate synchronizer, bref, pas grand chose !) et j'ai téléchargé les librairies nécessaires au "bon" fonctionnement d'Hibernate (et sans maven, c'est parfois un peu rigolo ^^).
Je fais simple pour l'explication du projet :
La classe dont tout mon domaine hérite, la classe ObjetProjet :
Une classe adresse, héritant donc de ObjetProjet :
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 package com.projet.domaine; public class ObjetProjet { private int reference; public int getReference() { return reference; } public void setReference(int aReference) { reference = aReference; } public ObjetProjet() { } }
J'ai bien sur un 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 package com.projet.domaine; public class Adresse extends ObjetProjet { private String numeroDeRue; private String rue; private String ville; private String codePostal; private String pays; public String getNumeroDeRue() { return numeroDeRue; } public void setNumeroDeRue(String aNumeroDeRue) { numeroDeRue = aNumeroDeRue; } public String getRue() { return rue; } public void setRue(String aRue) { rue = aRue; } public String getVille() { return ville; } public void setVille(String aVille) { ville = aVille; } public String getCodePostal() { return codePostal; } public void setCodePostal(String aCodePostal) { codePostal = aCodePostal; } public String getPays() { return pays; } public void setPays(String aPays) { pays = aPays; } }
Mon fichier de mapping Adresse.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 <?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"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://etourneaumo.no-ip.biz/Projet</property> <property name="hibernate.connection.username">etourneaumo</property> <property name="hibernate.connection.password">MotDeP@sse</property> <mapping resource="com/projet/domaine/Adresse.hbm.xml"/> </session-factory> </hibernate-configuration>
Et enfin, mon main :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.projet.domaine.Adresse" table="_Adresse"> <id name="reference" type="int" column="reference"> <generator class="native"/> </id> <property name="numeroDeRue" type="string" not-null="false" /> <property name="rue" type="string" not-null="false" /> <property name="ville" type="string" not-null="false" /> <property name="codePostal" type="string" not-null="false" /> <property name="pays" type="string" not-null="false" /> </class> </hibernate-mapping>
Bon, et avec tout ça, quand je lance fièrement ma classe Test, j'ai cette erreur dans mes logs :
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 package com.projet; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.projet.domaine.Adresse; public class Test { /** * @param args */ public static void main(String[] args) { Configuration config = new Configuration() .configure("hibernate.cfg.xml"); config.addClass(Adresse.class); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); Adresse vAdresse = new Adresse(); vAdresse.setCodePostal("27350"); vAdresse.setNumeroDeRue("5"); vAdresse.setPays("FRANCE"); // vAdresse.setReference(1); vAdresse.setRue("rue de la Mare Mande"); vAdresse.setVille("ROUTOT"); session.save(vAdresse); session.close(); } }
J'ai bien vérifié : je n'ai en entrée (dans mes sources) et en sortie (répertoire bin) qu'un seul fichier Adresse.java/Adresse.class et qu'un seul Adresse.hbm.xml dans mes sources et un Adresse.hbm.xml avec mon .class.SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2013-11-08 19:13:08 INFO Version:37 - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/projet/domaine/Adresse.hbm.xml
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3951)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3940)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3928)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1368)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
at com.projet.Test.main(Test.java:18)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.projet.domaine.Adresse
at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:3121)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:175)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3948)
... 5 more
Une idée pour quelqu'un ?
Partager