Problème d'initalisation d'HibernateUtil.java
Bonjour à tous
Depuis 3 jours je arpente les forums en quêtes d'informations relatives à mon souci d'initialisation de la class HibernateUtil.java
Malgré plusieurs réponses découvertes et intéressantes, je n'arrive pas à le résoudre.
Je suis sur que c'est pas grand chose mais ........ voilà je bloque
Auriez vous une solution à m'apporter ?
Merci par avance pour la débutante que je suis :oops:
Voici ma configuration :
Base de données MYSQL
Eclipse Helios
Hibernate 3
Serveur Jboss 6 (test fait également avec un serveur tomcat 6)
la base de données contient une simple table client de 3 champs : id (en autoincrément), nom et prenom en varchar 50.
J'ai un fichier index.php qui présente un formulaire pour insérer un nouveau client.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ">
<html>
<head>
<title>Insert title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h3>Formulaire d'insertion</h3>
<form action="Insertion" method="get">
<label title="Nom"> Nom :</label><input type="text" name="nom"><br>
<label title="Prenom">Prenom :</label><input type="text" name="prenom"><br>
<input type="submit">
</form>
</body>
</html> |
Un fichier Insertion.java qui gère l'insertion du client.
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
|
package com.traitement;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.db.Client;
import com.util.HibernateUtil;
public class Insertion extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//recupération des ddonnées depuis la page JSP
String nom=request.getParameter("nom");
String prenom=request.getParameter("prenom");
//Construction l'object client
Client client=new Client();
client.setNom(nom);
client.setPrenom(prenom);
//Declaration d'un objet Transaction
Transaction tx=null;
try{
//obtention de session hibernate
Session session = HibernateUtil.currentSession();
//debut transaction
tx=session.beginTransaction();
//persistance de l'objet client
session.save(client);
//validation de la transaction
tx.commit();
//fermeture session
HibernateUtil.closeSession();
}catch(Exception e){
System.out.println("erreur de la transaction"+e.getMessage());
tx.rollback();
}
}
} |
Un fichier HibernateUtil.java qui gère le session-factory.
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
|
package com.util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Crée la SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de configuration : "+ ex.getMessage(), ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
throws HibernateException {
Session s = (Session) session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
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();
}
} |
Et bien sur tout les fichiers de config nécessaire à la liaison entre la bdd et le fichier objet.
Client.hbm.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 9 juil. 2012 15:44:21 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.db.Client" table="client" catalog="hibdbexemple">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="nom" type="string">
<column name="nom" length="50" not-null="true" />
</property>
<property name="prenom" type="string">
<column name="prenom" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping> |
Client.java
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
|
package com.db;
// Generated 9 juil. 2012 15:44:20 by Hibernate Tools 3.4.0.CR1
/**
* Client generated by hbm2java
*/
public class Client implements java.io.Serializable {
private Integer id;
private String nom;
private String prenom;
public Client() {
}
public Client(String nom, String prenom) {
this.nom = nom;
this.prenom = prenom;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return this.prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
} |
Hibernate.cfg.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
<?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.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibdbexemple</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="com/db/Client.hbm.xml" />
</session-factory>
</hibernate-configuration> |
Hibernate.reveng.xml
Code:
1 2 3 4 5 6 7
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-name="client" match-catalog="hibdbexemple"></table-filter>
</hibernate-reverse-engineering> |
et voici le web.xml
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
|
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>HibernateInstallation</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>insertion.html</welcome-file>
<welcome-file>insertion.htm</welcome-file>
<welcome-file>insertion.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Insertion</display-name>
<servlet-name>Insertion</servlet-name>
<servlet-class>com.traitement.Insertion</servlet-class>
</servlet>
<servlet>
<description></description>
<display-name>Lister</display-name>
<servlet-name>Lister</servlet-name>
<servlet-class>com.traitement.Lister</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Insertion</servlet-name>
<url-pattern>/Insertion</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Lister</servlet-name>
<url-pattern>/Lister</url-pattern>
</servlet-mapping>
</web-app> |
Voici les librairies que j'ai dans le WEB-INF/lib :
- antlr-2.7.6.jar
- cglib-2.2.jar
- commons-collections-3.1.jar
- dom4j-1.6.1.jar
- hibernate3.jar
- jta-1.1.jar
- log4j-1.2.15.jar
- mysql-connector-java-5.0.08-bin.jar (également sous le répertoire du serveur jboss)
- slf4j-api-1.5.8.jar
- slf4j-jdk14-1.5.8.jar
On les retouvres dans le java build path du projet ainsi que :
- EAR Librairies
- JBoss 6.0 runtime
- JRE System Library
- Web App Librairies
J'ai créer un fichier test.java qui contient une méthode main pour tester l'insertion d'un client en ligne de commande. Lorsque je lance ce fichier en "Run as", "java application" aucun souci, j'ai bien mes clients dans la base de données.
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
|
import org.hibernate.*;
import com.db.Client;
import com.util.*;
public class Test {
public static void main(String[] args)
throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Client contact = new Client();
contact.setNom("Dupont");
contact.setPrenom("Jean");
session.save(contact);
contact = new Client();
contact.setNom("Lambert");
contact.setPrenom("Julie");
session.save(contact);
tx.commit();
HibernateUtil.closeSession();
}
} |
Lorsque je lance le fichier index.php en "Run as", "run on Server", il m'affiche mon formulaire de saisie.
Je saisie un nom, un prénom et je valide et c'est la que j'ai mon souci.
J'ai une première erreur de ce type
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
|
Etat HTTP 500 -
type Rapport d'exception
message
description Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.
exception
javax.servlet.ServletException: L'exécution de la servlet a lancé une exception
cause mère
java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1385)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
com.util.HibernateUtil.<clinit>(HibernateUtil.java:13)
com.traitement.Insertion.doGet(Insertion.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
cause mère
java.lang.ClassNotFoundException: javax.persistence.EntityListeners
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1385)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
com.util.HibernateUtil.<clinit>(HibernateUtil.java:13)
com.traitement.Insertion.doGet(Insertion.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/6.0.14.
Apache Tomcat/6.0.14 |
Puis si je fais un F5 pour rafraîchir ma page j'obtiens cette erreur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
Etat HTTP 500 -
type Rapport d'exception
message
description Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.
exception
javax.servlet.ServletException: L'exécution de la servlet a lancé une exception
cause mère
java.lang.NoClassDefFoundError: Could not initialize class com.util.HibernateUtil
com.traitement.Insertion.doGet(Insertion.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/6.0.14.
Apache Tomcat/6.0.14 |
Et voici l'erreur en console :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
11 juil. 2012 10:35:30 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: "Servlet.service()" pour la servlet Insertion a généré une exception
java.lang.NoClassDefFoundError: Could not initialize class com.util.HibernateUtil
at com.traitement.Insertion.doGet(Insertion.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source) |
Help please :cry: