bonjour tout monde
j ai utilisé Hibernate et voici le code de ma classe HibernateUtil.java

voici message d'erreur
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
 
  17-janv.-2012 21:07:58 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.0.1
17-janv.-2012 21:07:58 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
17-janv.-2012 21:07:58 org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
17-janv.-2012 21:07:58 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
17-janv.-2012 21:07:58 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
17-janv.-2012 21:07:58 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
17-janv.-2012 21:07:58 org.hibernate.cfg.Configuration getConfigurationInputStream
ATTENTION: /hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.vente.impldao.UserImpl.findAll(UserImpl.java:33)
	at TestHibernate.main(TestHibernate.java:9)
Caused by: java.lang.RuntimeException: probleme de configuration  :/hibernate.cfg.xml not found
	at com.vente.util.HibernateUtil.<clinit>(HibernateUtil.java:15)
	... 2 more
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
	at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1137)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:1148)
	at com.vente.util.HibernateUtil.<clinit>(HibernateUtil.java:12)
	... 2 more
voici classe HibernateUtil
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.vente.util;
import org.hibernate.Hibernate;
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 e){
		  throw new RuntimeException("probleme de configuration  :" +e.getMessage(),e);}
   }
  private  static final ThreadLocal session = new ThreadLocal();
 
  public static Session  getOpenSession(){
	   Session s = (Session)session.get();
	   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();
    	session.set(null);
    }
}
:
voici classe 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
15
16
 
import java.util.ArrayList;
import com.vente.*;
import com.vente.beans.User;
import com.vente.impldao.UserImpl;
public class TestHibernate {
  public static void main(String[] args) {
		UserImpl unser = new UserImpl();
		ArrayList<User> u =unser.findAll();
		for(User user :u){
			System.out.println(" User : " +user.getUsername());
		}
 
	}
 
}
fichier xml 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
 
 <?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.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/vente</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.transaction.factory_class">
        org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="com/vente/beans/User.hbm.xml"/>
 
    </session-factory>
</hibernate-configuration>
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
 
  package com.vente.impldao;
 
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
import com.vente.beans.User;
import com.vente.dao.UserDao;
import com.vente.util.HibernateUtil;
 
public class UserImpl implements UserDao {
	private Log log = LogFactory.getLog(UserImpl.class);
	public void add(User obj) {
		Transaction tx =null;
		try{
			log.debug("User :"+obj);
			Session session =HibernateUtil.getOpenSession();
			tx=session.beginTransaction();
			session.save(obj);
			tx.commit();
	    }catch(Exception e){log.error("add user  failed :"+e);};
	    HibernateUtil.closeSession();
	}
 
	public ArrayList<User> findAll() {
		ArrayList<User> result=null;
		Transaction tx =null;
	    Session session =HibernateUtil.getOpenSession();
		tx=session.beginTransaction();
		try{
		   result =(ArrayList<User>)session.createQuery("select from user").list();
		}catch(HibernateException e)
		{log.error("find user failed :" +e);
		e.printStackTrace();
		session.beginTransaction().rollback();
		};
 
		return result;
 
	}
 
	public User findByid(long id) {
		// TODO Auto-generated method stub
		return null;
	}
 
	public void remove(User obj) {
		// TODO Auto-generated method stub
 
	}
 
	public void update(User obj) {
		Session session =HibernateUtil.getOpenSession();
		Transaction tx =session.beginTransaction();
		try{
		  session.saveOrUpdate(obj);
		  tx.commit();
		}catch (Exception e) {log.error("Update user  failed :"+e);}
 
	}
 
}