Bonjour, voila je debut sur hibernate (version5.3) avec Eclipse, je vous montre le code que j'ai fait :
Mon main :
Mon fichier HIbernateUtil.java
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 package modele; import org.hibernate.Session; import org.hibernate.Transaction; public class application { public static void main(String[] args) { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); Etudiant eleve1 = new Etudiant(); eleve1.setNom("Dupont"); eleve1.setPrenom("Jacques"); eleve1.setAge(40); session.save(eleve1); tx.commit(); // TODO Auto-generated method stub HibernateUtil.closeSession(); } }
Mon etudiant.java
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 package modele; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (HibernateException ex) { throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex); } } public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // Open a new Session, if this Thread has none yet if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); session.set(null); if (s != null) s.close(); } }
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 package modele; public class Etudiant { private int idEtudiant; private String nom; private String prenom; private int age; public int getIdEtudiant() { return idEtudiant; } public void setIdEtudiant(int idEtudiant) { this.idEtudiant = idEtudiant; } public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public String getPrenom() { return prenom; } public void setPrenom(String prenom) { this.prenom = prenom; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
mapping Etudiant.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 <?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <hibernate-configuration> <session-factory > <property name="show_sql">true</property> <property name="hibernate.connection.driver_class">DRIVER DE LA BASE</property> // je ne le met pas ici pour securite <property name="hibernate.connection.url">URL DE LA BASE DE DONNEES</property>// je ne le met pas ici pour securite <property name="hibernate.connection.username">stag11</property> <property name="hibernate.connection.password">stag11pw</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <mapping class="modele.Etudiant"/> </session-factory> </hibernate-configuration>
Lorsque je lance le main j'ai une erreur : Exception in thread "main" org.hibernate.MappingException: Unknown entity: modele.Etudiant, je n'arrive à trouver d'où peut provenitr les erreurs ( pour infos : le main , la classe etudiant , hibernateUtil et etudiant.hbm.xms sont dans un package modele dans src, et la config hibernate directement sous src)
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://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 18 f?vr. 2019 ? 15:47:21 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="modele.Etudiant" table="ETUDIANT"> <id name="idEtudiant" type="int"> <column name="ID_ETUDIANT" /> <generator class="assigned" /> </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="age" type="int"> <column name="AGE" /> </property> </class> </hibernate-mapping>
Merci de vos reponses
J'utilise une base Oracle avec juste une table ETUDIANT ( ID_ETUDIANT,NOM,PRENOM,AGE)
Partager