0

I have configured hibernate in my dispatcher servlet. The program starts fine. But when creating a Configuration object and configuring it by calling the configure() method, the error is thrown:

 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) 

How can I get the Configuration object to configure from the dispatcher servlet where the hibernate configuration is defined?

The method that builds the sessionfactory is as follows:

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); } } 

The dispatcher-servlet code is as follows:

<?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> 
1
  • 1
    Please put your applicationContext.xml. I want to see how your are initializing hibernate part Commented Apr 3, 2013 at 10:46

3 Answers 3

1

Through you have integrated hibernate to spring why do you still use hibernate to build sessionFactory, in other words, you don't have to use hibernate.cfg.xml there is a sessionFactory bean already in the applicationContext.xml, just use annotation to get it's instance. then enjoy it:)

see your another question for my answer SessionFactory injection isn't working

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I scrapped off the method with Configuration object. I tried to make the changes. I have removed 'static' from both the method and variable but still the sessionFactory is null.
0

root cause is org.hibernate.HibernateException: /hibernate.cfg.xml not found. By default Hibernate configuration manager try to find cfg file in WEB-INF/classes, put your hibernate.cfg.xml in WEB-INF/classes foldern and it should work.

Edit::

You are trying to buid session factory by own hence it is just like typical hibernate application. In this case hibernate.cfg.xml will be required. But you can see there is session factory bean in application-contex.xml. When spring loads the xml then hibernate.cfg.xml is not requıred because session factory is already defined in application-context and it will be created.

6 Comments

it doesn't exist as i have incorporated the hibernate.cfg.xml code in the dispatcher servlet in the form of beans.
Can you edit the question and put some code how you are doing initialization
@nick-s I have added modified answer
I figured I would have to use HibernateTemplate instead of Session to interact with database which I don't want to do. I have already implemented my system using Session object. I wonder if it is possible to interact with the database bypassing the use of HibernateTemplate.
You want sessin object from Spring session factory ?
|
0

Yes you are right. why you need configuration file if you are providing it in dispatcher.

but your accessing session factory is wrong.

"Configuration configuration = new Configuration().configure();" will always go for

Configuration file. You need to access it by HibernateTamplate which is provided by Spring.

please check following link to solve your error.

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.