Bonjour,
Actuellement débutant sur hibernate et en train de faire un petit exemple de test, je suis confronté à une erreur que je n'arrive pas à résoudre.
J'ai fait un exemple très simple de servlet qui execute une action insérant une personne dans ma table user. Je gère les servlets avec struts.
Voici mon erreur lorsque j'exécute mon action :
La ligne 31 de TestHibernateAction.java pose apparament un problème.
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 type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: Servlet execution threw an exception at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193) at com.sas.webapp.servlet.filters.UrlReplayBlockerFilter.doFilter(UrlReplayBlockerFilter.java:95) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193) at com.sas.servlet.filters.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:62) [...] root cause java.lang.NoClassDefFoundError: net/sf/hibernate/HibernateException at testHibernate.TestHibernateAction.execute(TestHibernateAction.java:31) at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58) at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67) at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51) at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190) at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304) [...]
Ci-dessous le code ma classe d'action avec sa méthode execute :
J'utilise une classe HibernateUtil que j'ai trouvé sur internet pour gérer la création de la session.
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 package testHibernate; [...] public class TestHibernateAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception { DynaActionForm daf = (DynaActionForm)form; Session session = HibernateUtil.currentSession(); // ligne 31 Transaction tx = session.beginTransaction(); try { User user = new User(); user.setLogin("jdupont"); user.setLastname( "Dupont" ); user.setFirstname("Jean"); session.save(user); tx.commit(); } finally { HibernateUtil.closeSession(); } return mapping.findForward("succes"); } }
Ci-dessous mon fichier de configuration de hibernate :
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 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(); } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Est ce quelqu'un aurait une idée de la provenance du problème ?
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 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <!-- local connection properties --> <property name="hibernate.connection.url"> jdbc:mysql://localhost/darties </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password" /> <!-- property name="hibernate.connection.pool_size"></property --> <!-- dialect for MySQL --> <property name="dialect"> net.sf.hibernate.dialect.MySQLInnoDBDialect </property> <property name="hibernate.show_sql">false</property> <property name="hibernate.use_outer_join">true</property> <mapping resource="hibernate/Request.hbm" /> <mapping resource="hibernate/User.hbm" /> </session-factory> </hibernate-configuration>
Merci d'avance et bonne journée.
Partager