Problème de Session. Eclipse + Hibernate3 + Oracle v9
Bonjour,
J'un petit soucis de'utilisation d'hibernate... En effet, j'arrive à faire mon fichier de config et mes fichiers de mapping. Je crée un Fichier HibernateUtil tel que :
Code:
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
|
package fr.mfi.connect;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
public static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();
public static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>();
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session currentSession() throws HibernateException {
Session s = threadSession.get();
if (s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = threadSession.get();
threadSession.set(null);
if (s != null)
s.close();
}
public static void beginTransaction() {
Transaction tx = threadTransaction.get();
if(tx == null) {
tx = currentSession().beginTransaction();
threadTransaction.set(tx);
}
}
public static void commitTransaction() {
Transaction tx = threadTransaction.get();
try {
if(tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
threadTransaction.set(null);
} catch(HibernateException ex) {
rollbackTransaction();
throw ex;
}
}
public static void rollbackTransaction() {
Transaction tx = threadTransaction.get();
threadTransaction.set(null);
try {
if(tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} finally {
closeSession();
}
}
} |
Je crée ensuite un petit fichier test pour tester le programme. Je fais un petit "SELECT" pour afficher deux ou trois trucs manière de voir si tous ce connecte comme il faut...
Programme de test :
Code:
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
|
package fr.mfi.test;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import fr.mfi.ToolsTest2.City;
import fr.mfi.connect.HibernateUtil;
public class Test {
public static void main(String[] args)
throws HibernateException {
/************Lecture de données à la base*****************/
Session session = HibernateUtil.currentSession();
List list = session.createQuery("from CITY where ID_DISTRICT = '31'").list();
Iterator it = list.iterator();
while(it.hasNext())
{
City MP = (City)it.next();
System.out.println(MP.getCity());
}
HibernateUtil.closeSession();
}
} |
Et quand je lance le programme il me sort :
Code:
1 2 3 4 5 6 7 8
|
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/dom4j/DocumentException
Exception in thread "main" java.lang.ExceptionInInitializerError
at fr.mfi.connect.HibernateUtil.<clinit>(HibernateUtil.java:110)
at fr.mfi.test.Test.main(Test.java:61)
Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at fr.mfi.connect.HibernateUtil.<clinit>(HibernateUtil.java:106)
... 1 more |
HibernateUtil.java:110 -> [...]throw new ExceptionInInitializerError(ex);[...]
Test.java:61 -> [...]Session session = HibernateUtil.currentSession();[...]
HibernateUtil.java:106 -> [...]sessionFactory = new Configuration().configure().buildSessionFactory();[...]
Voilà voilà...
Est ce que quelqu'un a une solution miracle??? ou pas miracle mais solution quand même???
Merci!