This question already has an answer here:
I am having issues with an HibernateUtil class . Here is its code:
package org.hibernate;
import java.util.ArrayList;
import java.util.List;
import org.entities.Rats;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
private static org.hibernate.service.ServiceRegistry serviceRegistry;
static
{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
factory = configuration.configure().buildSessionFactory(serviceRegistry);
}catch (HibernateException e) {
e.printStackTrace();
}
System.out.println("Static initializer. Factory : "+factory);
}
public static SessionFactory getSessionFactory()
{
return factory;
}
If I call it into the main method of a java class, it works fine:
public static void main(String[] args) {
// TODO Auto-generated method stub
Session session=null;
try{
HibernateUtil.getSessionFactory();
// session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
// logic here
tx.commit();
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
if(session != null)
session.close();
}
}
But if I call the exact same code into the doGet method of a servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//same code than before trying to do HibernateUtil.getSessionFactory();
}
The debugger reaches line HibernateUtil.getSessionFactory(); and then immediately switches to the finaly block.
Does anybody understand the reason why??
I assumed it could be an issue with the static initializer, so I tried to make the getSessionFactory independant of the initializer this way but the problem still occurs:
public static SessionFactory getSessionFactory()
{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
factory = configuration.configure().buildSessionFactory(serviceRegistry);
}catch (HibernateException e) {
e.printStackTrace();
}
System.out.println("Static initializer. Factory :"+factory);
}
return factory;
}
The class (where the code works) and the servlet (where it does not work are in the same package and the import are apparently the same).
After reaching the finally block, the servletgenerates he following:
HTTP Status 500 - Servlet execution threw an exception
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: org/hibernate/HibernateException
org.servlets.test.Test.doGet(Test.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: org.hibernate.HibernateException
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
org.servlets.test.Test.doGet(Test.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
thx in advance.
Aucun commentaire:
Enregistrer un commentaire