首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Hibernate保存在Servlet中处理的数据时遇到错误

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

Stack Overflow用户
提问于 2012-11-24 13:32:49
回答 3查看 1.3K关注 0票数 0

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

代码语言:javascript
运行
复制
    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。(完整代码如下)

代码语言:javascript
运行
复制
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类

代码语言:javascript
运行
复制
@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类

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<?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>
EN

Stack Overflow用户

发布于 2012-11-24 15:22:34

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

你必须告诉我们你是如何构建/部署的(你是在使用maven吗?您是否正在通过eclipse打包war并将其上传到服务器?Hibernate库位于何处,类路径是否知道它?等等...)才能搞清楚这件事。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13538579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档