为什么这个Hibernate servlet会抛出IllegalAccessError异常?

时间:2009-06-30 09:42:44

标签: hibernate netbeans

我尝试使用Hibernate在NetBeans中创建一个非常小的Web应用程序。我在{{1}下放置了hibernate3.jarlog4J-1.2.14.jarsl4j.api.jarsl4j-log4ja2-1.5.6.jardom4j-1.6.1.jarjta.jarehcache.jar }文件夹。我还将Libraries放在hibernate.cfg.xml文件夹的default包下,默认source文件和log4j.properties放在Book.hbm.xml文件夹的另一个包下。我不知道这是对还是错。我为Source和控制器servlet写了Hibernate Manager类,如下所示。但是如果我运行这个servlet,我会得到一个异常。

控制器servlet:

sessionfactory

管理器:

import java.io.*;
import java.net.*;
import Syron.HibernateManager;
import Syron.BookInfo;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.List;

public class ControllerServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HibernateManager manager = new HibernateManager(BookInfo.class);
        List categories = manager.findAll();
        request.setAttribute("categories", categories);
        ServletContext context = getServletContext();
        try
        {
            RequestDispatcher dispatcher = context.getRequestDispatcher("/welcomeJSF.jsp");
            dispatcher.forward(request, response);
        }
        catch(Exception es)  {

        }
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse   response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

     public String getServletInfo() {
        return "Short description";
    }

}

例外:

import java.util.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.util.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

//import org.slf4j.impl.*;

public class HibernateManager {
    private Class _cl;

    public  HibernateManager(Class cl)
    {
        _cl = cl;
    }

    public List findAll() throws HibernateException
    {
        List rv = new ArrayList();

        SessionFactory sessionFactory;
        try
        {
            Configuration config = new Configuration();
            config = config.configure("/hibernate.cfg.xml");
            sessionFactory = config.buildSessionFactory();
        }
        catch (HibernateException ex)
        {
            throw new RuntimeException("Exception building SessionFactory: " +ex.getMessage(), ex);
        }

        rv = sessionFactory.getCurrentSession().createCriteria(BookInfo.class).list();
        return rv;
    }

    public BookInfo findById(int id) throws HibernateException
    {
        Session session = SessionProvider.currentSession();
        BookInfo rv = (BookInfo) session.get(_cl, new Integer(id));
        return rv;
    }
}

我尝试了各种各样的东西,但无法解决这个问题。我哪里做错了?我还放置了java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60) org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152) Syron.HibernateManager.findAll(HibernateManager.java:34) ControllerServlet.processRequest(ControllerServlet.java:40) ControllerServlet.doGet(ControllerServlet.java:62) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 文件而不是commons-logging.jar,但后来我sl4j.jar的{​​{1}}例外。所以我搜索并替换No class definition found并再次尝试如上所述。如何在NetBeans中使用Hibernate?

1 个答案:

答案 0 :(得分:0)

stacktrace中最明显的问题是:

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory

答案在this other SO question