使用Hibernate保存在Servlet中处理的数据时遇到错误

时间:2012-11-24 05:32:49

标签: java hibernate jsp servlets

大家好,我正在通过构建一个简单的Web应用程序表单来练习hibernate和servlet。如果我不使用hibernate来保存数据并且只使用JSP和servlet,那么该程序可以正常工作。现在如果我添加我的Hibernate代码就会产生这个错误。

    SEVERE: Servlet.service() for servlet [com.ApplicationForm.ApplicationForm] in context with path [/ApplicationForm] threw exception [Servlet execution threw an exception] with root cause
    java.lang.ClassNotFoundException: org.hibernate.HibernateException
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
        at com.ApplicationForm.AppFormServletHelper.doPost(AppFormServletHelper.java:54)
        at com.ApplicationForm.ApplicationForm.doPost(ApplicationForm.java:24)
//More errors messages here 

错误消息始终指向包含我的Hibernate实例的此类 class AppFormServletHelper。 (下面的完整代码)

public class AppFormServletHelper extends ApplicationFormServletBase {

    private static AppDetails applicant = new AppDetails();
    private static SaveApplicantData list = new SaveApplicantData();


    public AppFormServletHelper(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response){
        super(servlet, request, response);

    }

    public void doPost() throws ServletException, IOException{
        int userID=0;
        String stringUserID, firstName, lastName, address, mobile, landLine;
        String sex, university;


        stringUserID = request.getParameter((Integer.toString(userID)));
        firstName = request.getParameter("firstName");
        lastName = request.getParameter("lastName");
        address = request.getParameter("address");
        mobile = request.getParameter("mobile");
        landLine = request.getParameter("landLine");
        sex = request.getParameter("sex");
        university = request.getParameter("university");
        //more request.getParameter lines here

        recordApplicantDetails(userID, firstName, lastName, address, mobile, landLine, sex);
        recordApplicantEducation(university, uniDateGrad, hsGrad, hsDateGrad, elem, elemDateGrad);
        recordApplicantEmployment(companyN, jobRole, jobResp);

        //Save data using hibernate
**##Error pointing to this line**
        **HibernateSaveData save = new HibernateSaveData();
        save.beginHibernateTransaction(applicant);**

        list.addApplicantData(applicant);
        list.setApplicantData(applicant, list);

        request.setAttribute("AppDetails", applicant);

        RequestDispatcher dispatch = request.getRequestDispatcher("/WEB-INF/UserDetails.jsp");
        dispatch.forward(request, response);

    }
//Implementation of recordApplicantDetails method
//Implementation of recordApplicantEducation method
//Implementation of recordApplicantEmployment method

}

这是ApplicationFormServletBase类

@WebServlet("/appFormServlet")
public class ApplicationForm extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        AppFormServletHelper controllerHelper = new AppFormServletHelper(this,req, resp);
        controllerHelper.doPost();
    }

这是我的HibernateSaveData类

public class HibernateSaveData {

    private Session session; 

    public void beginHibernateTransaction(AppDetails details){
        session = new HibernateFactoryUtil().getSessionFactory().openSession();
        try{
            session.beginTransaction();
            session.save(details);
            session.getTransaction().commit();

        }
        catch (HibernateException e){
            if (session.getTransaction()!=null){
                session.getTransaction().rollback();    
            }
            System.out.println("Hibernate exception occured: "+ e);
        }
        finally {
            session.close();
        }   
    }   
}

此外,我已经配置了我的hibernate.cfg.xml,并且我正在为servlet使用@WebServlet注释,因此我没有配置任何xml映射。我没有发布所有代码,但如果它有助于解决这个问题,我会把它们放好。谁能帮我这个?提前致谢。

更新

这是我设置文件夹的方式 enter image description here

这是我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/ApplicationForm</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.ApplicationForm.StoreData.AppDetails"/>

    </session-factory>

</hibernate-configuration>

3 个答案:

答案 0 :(得分:0)

  

构建项目并尝试重新编译类并检查是否   错误仍然存​​在

答案 1 :(得分:0)

问题似乎是在部署时Hibernate(在本例中为HibernateException)不在您的类路径中。 由于我假设您使用IDE(例如Eclipse)并且在IDE中没有任何编译错误,因此最有可能出现在构建/部署过程中。

你必须告诉我们你的构建/部署是什么(你使用maven吗?你是通过eclipse打包战争并将其上传到服务器?Hibernate库位于何处?类路径是否知道它?等......)以便深入了解它。

答案 2 :(得分:0)

确保WEB-INF / lib文件夹中有hibernate3.jar文件。