title: Hibernate and Spring sessionFactory null and db not created
body: <p>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.</p>
  
  <p>These are my configuration files:</p>
  
  <ul>
  <li><p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  &lt;display-name&gt;Fantacalcio&lt;/display-name&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;spring-mvc&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;spring-mvc&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring-database.xml,/WEB-INF/spring-mvc-servlet.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  </code></pre>
  
  <p></p>
  
  <ul>
  <li>spring-mvc-servlet.xml</li>
  </ul></li>
  </ul>
  
     
  
  <p>
          
          
      </p>
  
  <pre><code>&lt;tx:annotation-driven /&gt;
  
  &lt;context:component-scan base-package="org.fabrizio.fantacalcio.controller"/&gt;
  </code></pre>
  
  <ul>
  <li><p>spring-database.xml</p>
  
  <p>
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
      <a href="http://www.springframework.org/schema/beans" rel="nofollow">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans.xsd" rel="nofollow">http://www.springframework.org/schema/beans/spring-beans.xsd</a>
      <a href="http://www.springframework.org/schema/context" rel="nofollow">http://www.springframework.org/schema/context</a> <a href="http://www.springframework.org/schema/context/spring-context.xsd" rel="nofollow">http://www.springframework.org/schema/context/spring-context.xsd</a> 
      <a href="http://www.springframework.org/schema/tx" rel="nofollow">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx.xsd" rel="nofollow">http://www.springframework.org/schema/tx/spring-tx.xsd</a>
      <a href="http://www.springframework.org/schema/security" rel="nofollow">http://www.springframework.org/schema/security</a> <a href="http://www.springframework.org/schema/security/spring-security.xsd" rel="nofollow">http://www.springframework.org/schema/security/spring-security.xsd</a>"></p>
  
  <pre><code>&lt;!-- Hibernate ..................................... --&gt;
  &lt;beans:bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;beans:property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;beans:bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;beans:property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/beans:bean&gt;
  
  
  &lt;!-- Miei Bean ...................................... --&gt; 
  
  &lt;beans:bean id="utenteDao"
      class="org.fabrizio.fantacalcio.model.dao.UtenteDaoImpl"&gt; 
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p></p></li>
  </ul>
  
  <p>In my BaseDaoImpl I have the following code:</p>
  
  <pre><code>@Repository
  public class BaseDaoImpl&lt;T&gt; implements BaseDao&lt;T&gt; {
  
  //  private static final Logger logger = LoggerFactory
  //          .getLogger(BaseDaoImpl.class);
  
      @Autowired
      private SessionFactory sessionFactory;
  
      private Session getSession() {
          return sessionFactory.getCurrentSession();
      }
  </code></pre>
  
  <p>The log generates these messages, resulting in null sessionFactory and no db file generated:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If someone could review my xml code formatting I would appreciate, as It doesn't show properly.</p>
  
  <p>EDIT: This is UtenteDaoImpl class, it does nothing as it inherits stuff from BaseDao.</p>
  
  <pre><code>@Repository
  public class UtenteDaoImpl extends BaseDaoImpl&lt;Utente&gt; implements UtenteDao{
  
  }
  </code></pre>