有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

大家好,我正在通过构建一个简单的web应用程序表单来练习hibernate和servlets。如果我不使用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实例的类 类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,我使用@WebServlet annotation for servlet,所以我没有配置任何xml映射。我没有发布所有的代码,但如果有助于解决这个问题,我会把它们贴出来。有人能帮我吗?提前谢谢

更新

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

这是我的冬眠。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>

共 (2) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

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