0

大家好,正在通过构建一个简单的 Web 应用程序表单来练习休眠和 servlet。如果我不使用休眠来保存数据而只使用 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 实例类 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 映射。我没有发布所有代码,但如果它有助于解决这个问题,我会发布它们。谁能帮我这个?提前致谢。

更新

这就是我设置文件夹的方式 在此处输入图像描述

这是我的 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>
4

3 回答 3

0

Build your project and try to re-compile your classes and check if error still exists

于 2012-11-24T06:10:52.323 回答
0

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

于 2012-11-29T09:09:38.020 回答
0

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

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

于 2012-11-24T07:22:34.060 回答