动态Web项目中的HibernateException

时间:2018-05-23 04:46:25

标签: java hibernate

My project structure is as follows:

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>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">sql123</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create </property>
        <mapping resource="/com/eql/model/Employee.hbn.xml" />
    </session-factory>
</hibernate-configuration>
&#13;
&#13;
&#13;

方法是:

public void addEmployee(Employee employee) {
    try {
     Configuration configuration = new Configuration();
     configuration.configure("${pageContext.request.contextPath}/hibernate.cfg.xml");

     SessionFactory sessionFactory=configuration.buildSessionFactory();
     Session session=sessionFactory.openSession();
     Transaction transaction =session.beginTransaction();
     session.save(employee);
     transaction.commit();

     System.out.println("\n\n Details Added");
    }
    catch(HibernateException e) {
         System.out.println(e.getMessage());
         System.out.println("error");
    }
}

Stack跟踪是:

May 23, 2018 9:53:11 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [EmployeeController] in context with path [/HibernateWebApp] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.hibernate.HibernateException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    at com.eql.controller.EmployeeController.doPost(EmployeeController.java:37)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我添加了所有必需的hibernate jar,我尝试了很多方法

 Configuration configuration = new Configuration();
     configuration.configure("${pageContext.request.contextPath}/hibernate.cfg.xml");
  1. 我写了hibernate.cfg.xml的完整路径。
  2. 我只写了hibernate.cfg.xml
  3. 有谁可以告诉我,我在这个项目中做错了什么?

    提前谢谢

1 个答案:

答案 0 :(得分:0)

更改web.xml中的版本后。它运作正常