I tried to create very small web app in NetBean using hibernate .I placed hibernate3.jar , log4J-1.2.14.jar,sl4j.api.jar,sl4j-log4ja2-1.5.6.jar,dom4j-1.6.1.jar, jta.jar, ehcache.jar under " Libraries" folder. I also place hibernate.cfg.xml under "default "package of "source" folder and default log4j.properties file and Book.hbm.xml under other package of"Source" folder. I don't know whether it is right or wrong. I wrote "Hibernate Manager " class for sessionfactory and controller servlet as follows. But if I run this servlet I get exception as follows .
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.(LoggerFactory.java:60) org.hibernate.cfg.Configuration.(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)
Please help me if you know where it is wrong. I tried various ways .I also place commons-logging.jar file instead of sl4j.jar but at this time I get "No class definition found" exceptions for "org.sl4j.impl.?". So I search and place sl4j-log4ja2-1.5.6.jar and tried again as above .Please tell me the way to use hibernate in netbeans step by step.