![](/img/trans.png)
[英]Encountered this problem/error while learning SpringBoot (Java, Hibernate, PostgreSql, JSON)
[英]Error encountered while using Hibernate to save data processed in Servlets
大家好,我通过构建一个简单的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>
生成您的项目,然后尝试重新编译您的类,并检查错误是否仍然存在
问题似乎是部署时Hibernate(在这种情况下为HibernateException)不在类路径中。 由于我假设您使用的是IDE(例如Eclipse),并且在IDE中没有任何编译错误,因此故障很可能是在您的构建/部署过程中造成的。
您必须告诉我们您的构建/部署方式(您使用的是Maven吗?您是通过eclipse打包战争并将其上载到服务器吗?Hibernate库位于何处,类路径是否知道它?等。 。),以便深入了解它。
确保在WEB-INF / lib文件夹中有文件hibernate3.jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.