I get some problem while trying to integrate Spring with Hibernate. From the log it seems the tables and the database is created, but I can't find the database file and while inspecting the sessionFactory I get nullpointer exception, and I can't really understand what's going on.
These are my configuration files:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Fantacalcio</display-name> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-database.xml,/WEB-INF/spring-mvc-servlet.xml</param-value> </context-param>
- spring-mvc-servlet.xml
<tx:annotation-driven />
<context:component-scan base-package="org.fabrizio.fantacalcio.controller"/>
spring-database.xml
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- Hibernate ..................................... --> <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <beans:property name="configLocation" value="classpath:hibernate.cfg.xml" /> </beans:bean> <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <beans:property name="sessionFactory" ref="sessionFactory" /> </beans:bean> <!-- Miei Bean ...................................... --> <beans:bean id="utenteDao" class="org.fabrizio.fantacalcio.model.dao.UtenteDaoImpl"> </beans:bean>
In my BaseDaoImpl I have the following code:
@Repository
public class BaseDaoImpl<T> implements BaseDao<T> {
// private static final Logger logger = LoggerFactory
// .getLogger(BaseDaoImpl.class);
@Autowired
private SessionFactory sessionFactory;
private Session getSession() {
return sessionFactory.getCurrentSession();
}
The log generates these messages, resulting in null sessionFactory and no db file generated:
18/06/2015 11:20:49 - DEBUG - (StatisticsInitiator.java:110) - Statistics initialized [enabled=true]
18/06/2015 11:20:54 - DEBUG - (SessionFactoryImpl.java:1339) - HHH000031: Closing
18/06/2015 11:20:54 - DEBUG - (ActiveManagementCoordinator.java:149) - The mbean com.mchange.v2.c3p0:type=PooledDataSource,identityToken=2saucp9a1nmzsq198qnlq|1bdc6b2 was not found in the registry, so could not be unregistered.
18/06/2015 11:20:54 - DEBUG - (ActiveManagementCoordinator.java:97) - C3P0Registry mbean unregistered.
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1022) - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:616) - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (AbstractPoolBackedDataSource.java:477) - com.mchange.v2.c3p0.PoolBackedDataSource@1bdc6b2 has been closed.
java.lang.Exception: DEBUG STACK TRACE for PoolBackedDataSource.close().
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:477)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:489)
at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:372)
at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:348)
at org.hibernate.c3p0.internal.C3P0ConnectionProvider.stop(C3P0ConnectionProvider.java:258)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:377)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:361)
at org.hibernate.cfg.Configuration$2.sessionFactoryClosed(Configuration.java:1924)
at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryClosed(SessionFactoryObserverChain.java:65)
at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1376)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:488)
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:258)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:900)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:907)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:908)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:884)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5035)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5687)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1591)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1580)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
18/06/2015 11:20:54 - DEBUG - (NewPooledConnection.java:646) - com.mchange.v2.c3p0.impl.NewPooledConnection@9be45 closed by a client.
java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:646)
at com.mchange.v2.c3p0.impl.NewPooledConnection.closeMaybeCheckedOut(NewPooledConnection.java:259)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:619)
at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:1024)
at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1049)
at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:1010)
at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:44)
at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1260)
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:627) - Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1027) - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@9be45
18/06/2015 11:20:54 - DEBUG - (BasicResourcePool.java:1022) - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@532db4
18/06/2015 11:20:54 - DEBUG - (C3P0PooledConnectionPool.java:616) - Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@532db4
18/06/2015 11:20:54 - DEBUG - (BootstrapServiceRegistryImpl.java:308) - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
18/06/2015 11:20:54 - DEBUG - (AbstractServiceRegistryImpl.java:406) - Implicitly destroying ServiceRegistry on de-registration of all child ServiceRegistries
If someone could review my xml code formatting I would appreciate, as It doesn't show properly.
EDIT: This is UtenteDaoImpl class, it does nothing as it inherits stuff from BaseDao.
@Repository
public class UtenteDaoImpl extends BaseDaoImpl<Utente> implements UtenteDao{
}
UtenteDaoImpl
– Jaiwo99