salut tout le monde
pour mon premier programme avec Hibernate, j'ai rencontré quelques problèmes :
avant de vous enbetter avec mes probmèmes voici uen explication de ce que je veux faire avec qlq codes :
l'application est toute bête (et oui c'est le premier programme), je veux recuperer une liste dans une base de données mySql en utilisant hibernate :
1- Fichier de Config :
2- Fichier de Mapping
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 <?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.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mabase</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- property name="hibernate.hbm2ddl.auto">update</property--> <!-- Mapping files --> <mapping resource="com/sfeir/client/user.hbm.xml"/> </session-factory> </hibernate-configuration>
3 - une Classe HibernateUtil : pour récupérer la session :
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 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.sfeir.client"> <class name="User"> <id name="id" type="String" column="ID"> <generator class="increment"></generator> </id> <property name="firstname"> <column name="USERNAME"></column> </property> <property name="lastname"> <column name="LASTNAME"></column> </property> <property name="password"> <column name="PASSWORD"></column> </property> <property name="adress"> <column name="ADRESS"></column> </property> </property> </class> </hibernate-mapping>
et enfin mon code :import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println(
"Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
voila l'erreur que j'obtiens :
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 public List<User> getAllUser() { // TODO Auto-generated method stub List list=null; Session session=null; System.out.println("bahh ! On est dans RPC" ); try{ session = HibernateUtil.getSessionFactory().openSession(); String SQL_QUERY =" SELECT username, lastname, password, adress FROM USER "; Query query = session.createQuery(SQL_QUERY); for(Iterator it=query.iterate();it.hasNext();){ User row=(User)it.next(); System.out.println("utilisateur = " +row.getFirstname()); list.add(row); } session.close(); } catch(Exception e){ System.out.println(e.getMessage()); } finally{ } return(list); }
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError:
org/dom4j/DocumentException
dzl si le code est assez long mais ca fait presque deux heure que j'essaye de résoudre ce problème sans rien !
merci d'avance![]()
Partager