salut tout le monde
je suis débutante en jee et on m a demandé de travailler sur un projet avec hibernate. d'abord je vais décrire mon environnement:
IDE: netbeans 6.1
SGBD: Mysql
Hibernate
j'ai commencer par faire une petite application web en utilisant hibernate, pour cela je me suis basée sur un exemple déjà réalisé mais juste avec une application java et non web.
mon application contient différentes classes:
1ere s'appelle HibernatePersistance, voici son code:
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
 
package usersPack;
 
 
import java.util.Iterator;
import java.util.List;
 
import org.apache.log4j.Logger; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 
import org.hibernate.HibernateException;
 
 
public class HibernatePersistance {
 
private static Logger logger ; 
	private static Session session;
	private static Transaction tx ;
 
	public HibernatePersistance() {
		logger = Logger.getLogger(HibernatePersistance.class); 
		session = null;
		tx = null;
	}
 
	public void open() throws Exception {
		logger.debug("--------------------open transaction--------------------------");
		try 
		{
			session = InitSessionFactory.getInstance().getCurrentSession(); 
			tx = session.beginTransaction();
 
		}catch (HibernateException e){
			e.printStackTrace(); 
			if (tx != null && tx.isActive()) tx.rollback(); 
		}
	}
 
	public void saveEntity(Object obj){
		logger.debug("---------------saveEntity--------------------");
		try	{
			session.save(obj);
		}
		catch (HibernateException e) { 
			e.printStackTrace(); 
			if (tx != null && tx.isActive()) tx.rollback(); 
		}
	}
 
	public void updateEntity(Object obj){
		logger.debug("---------------updateEntity--------------------");
		try	{
			session.update(obj);
		}
		catch (HibernateException e) { 
			e.printStackTrace(); 
			if (tx != null && tx.isActive()) tx.rollback(); 
		}
	}
 
	public void deleteEntity(Object obj){
		logger.debug("--------------deleteEntity--------------------");
		try	{
			session.delete(obj);
		}
		catch (HibernateException e) { 
			e.printStackTrace(); 
			if (tx != null && tx.isActive()) tx.rollback(); 
		}
	}
 
	public Object getElementById(int id, String TableName){ 
		List users = session.createQuery("select u from "+TableName+" as u where id="+id).list();
		Iterator iter = users.iterator();
		Object obj = iter.next();
		return obj; 
	}
 
	public void commit(){
		logger.debug("--------------commit transaction--------------------");
		try	{
			tx.commit();
		}
		catch (HibernateException e) { 
			e.printStackTrace(); 
			if (tx != null && tx.isActive()) tx.rollback(); 
		}
	}
 
	public void close() throws HibernateException{
		logger.debug("--------------close transaction--------------------");
		InitSessionFactory.close(); 
		//session.close();
	};
 
}
2eme classe: InitSessionFactory
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package usersPack;
 
import javax.naming.InitialContext; 
import org.apache.log4j.Logger; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.cfg.Environment;
 
public class InitSessionFactory {
 
	/** * Default constructor. */ 
	private InitSessionFactory() { } 
	/** 
         * * Location of hibernate.cfg.xml file. NOTICE: Location should be on the 
         * * classpath as Hibernate uses #resourceAsStream style lookup for its 
         * * configuration file. That is place the config file in a Java package - the 
         * * default location is the default Java package.<br> 
         * * <br> * Examples: <br> * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". 
         * * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> */
 
	private static String CONFIG_FILE_LOCATION = "/WebHibernateExp/src/java/hibernate.cfg.xml"; 
	/** The single instance of hibernate configuration */ 
	private static final Configuration cfg = new Configuration(); 
	/** The single instance of hibernate SessionFactory */ 
	private static org.hibernate.SessionFactory sessionFactory; 
	/** * initialises the configuration if not yet done and returns the current * instance * * @return */ 
	public static SessionFactory getInstance() { 
		if (sessionFactory == null) initSessionFactory(); 
		return sessionFactory; 
	}
 
	/** * Returns the ThreadLocal Session instance. Lazy initialize the *
         *  <code>SessionFactory</code> if needed. * * @return Session * 
         *  @throws HibernateException */ 
	public Session openSession() { 
		return sessionFactory.getCurrentSession(); 
	} 
	/** *
         *  The behaviour of this method depends on the session context you have * 
         *  configured. This factory is intended to be used with a hibernate.cfg.xml * 
         *  including the following property <property * name="current_session_context_class">thread</property> This would return * 
         *  the current open session or if this does not exist, will create a new * session * * @return */ 
	public Session getCurrentSession() { 
		return sessionFactory.getCurrentSession();
	}
 
	/** * initializes the sessionfactory in a safe way even if more than one thread * 
         * tries to build a sessionFactory */ 
	private static synchronized void initSessionFactory() { 
		/* * [laliluna] check again for null because sessionFactory may have been * 
		 * initialized between the last check and now * */ 
 		Logger log = Logger.getLogger(InitSessionFactory.class); 
		if (sessionFactory == null) {
 
			try { 
				cfg.configure(CONFIG_FILE_LOCATION); 
				String sessionFactoryJndiName = cfg .getProperty(Environment.SESSION_FACTORY_NAME); 
				if (sessionFactoryJndiName != null) { 
					cfg.buildSessionFactory(); 
					log.debug("get a jndi session factory"); 
					sessionFactory = (SessionFactory) (new InitialContext()) .lookup(sessionFactoryJndiName); 
			    } else{ 
			    	log.debug("classic factory"); 
			    	sessionFactory = cfg.buildSessionFactory(); 
			    } 
			} catch (Exception e) { 
				System.err .println("%%%% Error Creating HibernateSessionFactory %%%%"); 
				e.printStackTrace(); 
				throw new HibernateException( "Could not initialize the Hibernate configuration"); 
			} 
		} 
	}
 
	public static void close(){ 
		if (sessionFactory != null) sessionFactory.close(); 
		sessionFactory = null; 
	} 
 
 
}
et une autre pour le mappin
alors qand j'essaie d'executer le code de ma classe qui doit ajouter une entrée dans la base l'erreur suivante est générée:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 
%%%% Error Creating HibernateSessionFactory %%%%
org.hibernate.HibernateException: /WebHibernateExp/src/java/hibernate.cfg.xml not found
le chemin exacte de mon fichier hibernate.cfg.xml est le suivant:
/src/java/hibernate.cfg.xml sachant que mon apllication a pour nom WebHibernateExp.
si quelqu'un a une idée je lui serai reconnaissante, je suis prête à fournir des éclaircissement si nécessaire.
merci d'avance et bonne journée à tous.