我已在我的调度程序servlet中配置了hibernate。程序开始很好。但是,在创建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)
如何从定义了hibernate配置的调度程序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>
答案 0 :(得分:1)
通过你已经集成了hibernate到spring为什么你仍然使用hibernate来构建sessionFactory,换句话说,你不必使用hibernate.cfg.xml,在applicationContext.xml中已经有一个sessionFactory bean,只需使用得到它的实例的注释。然后享受它:)
看到你的另一个问题我的答案 SessionFactory injection isn't working
祝你好运!答案 1 :(得分:0)
根本原因是org.hibernate.HibernateException: /hibernate.cfg.xml not found
。默认情况下,Hibernate配置管理器尝试在WEB-INF/classes
中找到cfg文件,将hibernate.cfg.xml
放在WEB-INF/classes
foldern中,它应该可以正常工作。
编辑::
你正在尝试自己建立会话工厂,因此它就像典型的hibernate应用程序一样。在这种情况下,将需要hibernate.cfg.xml
。但您可以在session factory bean
中看到application-contex.xml
。当spring
加载xml
时,hibernate.cfg.xml
不会被要求,因为session factory
已经在application-context
中定义了它,并且会创建它。
答案 2 :(得分:0)
是的,你是对的。如果您在调度员中提供配置文件,为什么需要配置文件。
但您访问的会话工厂是错误的。
“配置配置=新配置()。configure();”总是会去
配置文件。您需要通过Spring提供的HibernateTamplate访问它。
请检查以下链接以解决您的错误。
http://www.javatpoint.com/hibernate-and-spring-integration
http://www.javabeat.net/2007/10/integrating-spring-framework-with-hibernate-orm-framework/