Bonjour,

Je suis confronté au fameux problème java.lang.IllegalStateException: Could not locate the famous SessionFactory in JNDI.

J'ai généré les pojo et DAO d'une bdd sous Mysql via hibernate tools et j'essaie d'utiliser les DAO pour requeter en base. J'utilise Hibernate 4.0, tomcat 7.0 et le driver JDBC (fichier mysql-connector-java-5.1.18-bin.jar) qui se trouve dans le repertoire /lib de tomcat.


Mon code de test de DAO (requête d'un utilisateur qui s'appelle "test"):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
	public void testerMoi(){
		// TODO Auto-generated method stub
 
		Utilisateur user = new Utilisateur();
		user.setNom("test");
 
		UtilisateurHome userDAO = new UtilisateurHome();
		List userList = userDAO.findByExample(user);
 
		System.out.println(userList);		
 
	}
Le DAO:
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
public class UtilisateurHome {
 
    private static final Log log = LogFactory.getLog(UtilisateurHome.class);
 
    private final SessionFactory sessionFactory = getSessionFactory();
 
    protected SessionFactory getSessionFactory() {
        try {
            //return (SessionFactory) new InitialContext().lookup("SessionFactory");
        	return (SessionFactory) new InitialContext().lookup("java:comp/env/jdbc/javatest");
        }
        catch (Exception e) {
            log.error("Could not locate the famous SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate the famous SessionFactory in JNDI");
        }
    }
...
Mon web.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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>HibernateTest2</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
 
	<resource-ref>
		<description>
		    Connection Pool: 
		    Resource reference to a factory for java.sql.Connection
		    instances that may be used for talking to a particular
		    database that is configured in the Context
		    configurartion for the web application.
  		</description>
		<res-ref-name>jdbc/javatest</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>
 
</web-app>
Mon context.xml dans le repertoire META-INF de ma webapp:
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
<?xml version='1.0' encoding='utf-8'?>
 
<Context>
 
	<Resource name="jdbc/javatest"
		auth="Container"
		type="javax.sql.DataSource"
		username="javauser" 
		password="javauser" 
		driverClassName="com.mysql.jdbc.Driver"
		url="jdbc:mysql://localhost/bddprofils2" 
		maxActive="8" 
		maxIdle="4" />
 
</Context>
Quelle configuration ai-je loupé ?
Merci.