0

我在调度程序 servlet 中配置了休眠。程序启动良好。但是在创建Configuration对象并通过调用该configure()方法进行配置时,会抛出错误:

    Apr 03, 2013 9:14:58 PM org.hibernate.cfg.Configuration configure
    INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
    Apr 03, 2013 9:14:58 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
    Error creating session: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    Apr 03, 2013 9:14:58 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/LibraryTest1] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
    org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1953)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1934)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
    at com.csu.library.mvc.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:24)
    at com.csu.library.mvc.dao.generic.GenericHibernateDao.getSession(GenericHibernateDao.java:29)
    at com.csu.library.mvc.dao.implementation.UserDaoImpl.getUserByUsername(UserDaoImpl.java:23)
    at com.csu.library.mvc.service.impl.UserServiceImpl.getUser(UserServiceImpl.java:46)
    at com.csu.library.mvc.service.impl.UserServiceImpl.login(UserServiceImpl.java:57)
    at com.csu.library.mvc.controller.UserController.homepage(UserController.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    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:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source) 

如何从定义休眠配置的调度程序 servlet 获取要配置的配置对象?

构建 sessionfactory 的方法如下:

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
    try {
        //Configuration configuration = new Configuration();
        Configuration configuration = new Configuration().configure();

        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }
    catch(HibernateException he) {
        System.err.println("Error creating session: " + he); 
        throw new ExceptionInInitializerError(he);
    }
}

dispatcher-servlet 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven/>

    <context:annotation-config/>

    <!-- Scans the package for contents -->
    <context:component-scan base-package="com.csu.library.mvc"/>

    <!-- Maps static resources like images, css, javascript files -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView"/>
        <property name = "prefix" value = "/WEB-INF/jsps/"/>
        <property name = "suffix" value = ".jsp"/>
    </bean>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/csu_library"/>
        <property name="username" value="csulibrary"/>
        <property name="password" value="csulibrary"/>

        <!-- Pool Properties -->
        <property name="initialSize" value="5" />
        <property name="maxIdle" value="5" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id = "hibernateProperties" class = "java.util.Properties">
        <constructor-arg index="0">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            </props>
        </constructor-arg>
    </bean>

    <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value = "com.csu.library.mvc"/>
        <property name="dataSource" ref = "dataSource"/>        
        <property name="hibernateProperties" ref = "hibernateProperties"/>

    </bean>

</beans>
4

3 回答 3

1

通过你已经集成了hibernate到spring为什么还要使用hibernate来构建sessionFactory,也就是说你不必使用hibernate.cfg.xml applicationContext.xml中已经有一个sessionFactory bean,使用注解来获取这是实例。然后享受它:)

请参阅您的另一个问题以获取我的答案 SessionFactory injection is not working

祝你好运!

于 2013-04-04T12:47:39.273 回答
0

根本原因是org.hibernate.HibernateException: /hibernate.cfg.xml not found。默认情况下,Hibernate 配置管理器尝试在 中查找 cfg 文件WEB-INF/classes,将您hibernate.cfg.xml的文件放入文件夹中,WEB-INF/classes它应该可以工作。

编辑::

您正在尝试自己构建会话工厂,因此它就像典型的休眠应用程序一样。在这种情况下hibernate.cfg.xml将需要。但是你可以看到里面session factory beanapplication-contex.xmlspring加载时不需要 then ,因为xml已在其中定义并将创建它。hibernate.cfg.xmlsession factoryapplication-context

于 2013-04-03T10:37:12.237 回答
0

是的你是对的。如果您在调度程序中提供配置文件,为什么需要配置文件。

但是您访问会话工厂是错误的。

“配置配置=新配置()。配置();” 将永远追求

配置文件。您需要通过 Spring 提供的 HibernateTamplate 来访问它。

请检查以下链接以解决您的错误。

http://www.javatpoint.com/hibernate-and-spring-integration

http://www.javabeat.net/2007/10/integrating-spring-framework-with-hibernate-orm-framework/

于 2013-04-03T11:29:49.223 回答