MVC模板项目没有看到hibernate.cfg.xml

时间:2012-12-09 15:30:05

标签: java spring hibernate spring-mvc hibernate.cfg.xml

我是Spring MVC的新手。我正在使用STS,我创建了Spring MVC模板项目(默认情况下使用Maven)。我正在尝试使用Hibernate,我知道在默认的Java Projects中我必须将hibernate.xml.cfg放在/ src文件夹中。我已经读过这个(在我的情况下使用带有maven的Spring MVC模板)我必须将hibernate.cfg.xml放在src/main/resources/WEB-INF/classes中。但它也没有帮助。我仍然有ClassNotFoundException:

    SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mvc] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
java.lang.ClassNotFoundException: org.dom4j.DocumentException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at com.andrew.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:18)
    at com.andrew.hibernate.DAO.getStudent(DAO.java:48)
    at com.andrew.mvc.HomeController.home(HomeController.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

这是我的hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

      <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433</property>
        <property name="connection.username">hibernate</property>
        <property name="connection.password">123456</property>

      <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

      <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

      <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</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>


      <!-- Mapping files -->
          <mapping class="com.andrew.ModelPackage.Student"/>
          <mapping class="com.andrew.ModelPackage.Subject"/>

    </session-factory>

</hibernate-configuration>

我的HibernateUtil类:

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {

        try {

            // Create the SessionFactory from hibernate.cfg.xml
            //AnnotationConfiguration
            sessionFactory = new Configuration().configure().buildSessionFactory();

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {

        return sessionFactory;

    }
}

也许我应该在web.xml或servlet-context.xml中写一些关于hibernate.cfg.xml的内容?我正在解决这个问题大约7个小时,我甚至尝试将hibernate.cfg.xml放在项目的每个文件夹中,但它没有帮助。

1 个答案:

答案 0 :(得分:1)

IMO对hibernate.cfx.xml的位置没有问题,但看起来像是一个丢失的库。您应该在类路径中添加dom4j.jar