netbeans中的Java Enterprise Web开发

时间:2017-06-01 02:46:27

标签: mysql spring hibernate registration netbeans-7

我正在尝试使用mysql spring hibernate框架构建一个简单的表单但是它给出了错误。我无法弄清楚为什么model.hibernateUtil没有初始化。请帮我。 这是hibernateUtil的代码: -

package model;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
public class HibernateUtil
{
    private static final SessionFactory sessionFactory;
    static
    {
       try
       {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
       } 
       catch (Throwable ex)
       {    
            System.err.println("Initial SessionFactory creation failed." +     ex);         throw new ExceptionInInitializerError(ex);
       }
    }
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
}

这是控制器的代码

package controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.HibernateUtil;
import org.hibernate.Session;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class UserController implements Controller
{
@Override
public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception 
{
    ModelAndView mv = new ModelAndView("user");
    String out = "All User Details: ";
    try
    {
        Session session HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List result = session.createQuery("from Users").list();
        mv.addObject("users", result);
        session.getTransaction().commit();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    mv.addObject("message", out);
    return mv;
    }
}    

这些是即将发生的例外/错误。

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class model.HibernateUtil
org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1284)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

java.lang.NoClassDefFoundError: Could not initialize class model.HibernateUtil
controller.UserController.handleRequest(UserController.java:18)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

请帮助我,我只是一个菜鸟。数据库连接和所有必需的驱动程序处于完美的工作状态。

我的数据库的名称是User,它有3个属性,名称为UserID,UserName,Password。

0 个答案:

没有答案