title: Spring 3.1 + hibernate 4: Can't get it to run
body: <p>I tried different configurations but to no effect. The error remained the same. Here the desired config taken from BoneCPs web site:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
  
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.2.RELEASE.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.2.RELEASE.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  "&gt;    
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire-candidate="" autowire="autodetect"&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.provider_class"&gt;com.jolbox.bonecp.provider.BoneCPConnectionProvider&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.driver_class"&gt;org.postgresql.Driver&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.url"&gt;jdbc:postgresql:MyDB&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.username"&gt;postgres&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.password"&gt;123456&lt;/prop&gt;
                  &lt;prop key="bonecp.idleMaxAge"&gt;240&lt;/prop&gt;
                  &lt;prop key="bonecp.idleConnectionTestPeriod"&gt;60&lt;/prop&gt;
                  &lt;prop key="bonecp.partitionCount"&gt;1&lt;/prop&gt;
                  &lt;prop key="bonecp.acquireIncrement"&gt;5&lt;/prop&gt;
                  &lt;prop key="bonecp.maxConnectionsPerPartition"&gt;60&lt;/prop&gt;
                  &lt;prop key="bonecp.minConnectionsPerPartition"&gt;5&lt;/prop&gt;
                  &lt;prop key="bonecp.statementsCacheSize"&gt;50&lt;/prop&gt;
                  &lt;prop key="bonecp.releaseHelperThreads"&gt;2&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;!--&lt;bean id="transactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt; --&gt;
  
      &lt;bean id="AbstractHibernateDAO" abstract="true"
            class="org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO"/&gt;
  
      &lt;bean id="ChemicalStructureDAO" extends="AbstractHibernateDAO"
            class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalStructureDAO"/&gt;
      &lt;bean id="ChemicalCompoundDAO" extends="AbstractHibernateDAO"
            class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalCompoundDAO"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>And code containing autowired session factory:</p>
  
  <pre><code>@Repository
  public abstract class AbstractHibernateDAO&lt; T extends Serializable&gt; {
  
      private final Class&lt; T&gt; clazz;
      @Autowired
      SessionFactory sessionFactory;
  
      public AbstractHibernateDAO(final Class&lt; T&gt; clazzToSet){
          this.clazz = clazzToSet;
      }
  
      public T getById(final Long id) {
          Preconditions.checkArgument(id != null);
          return (T) this.getCurrentSession().get(this.clazz, id);
      }
  
      public List&lt; T&gt; getAll() {
          return this.getCurrentSession()
                  .createQuery("from " + this.clazz.getName()).list();
      }
  
      public void create(final T entity) {
          Preconditions.checkNotNull(entity);
          this.getCurrentSession().persist(entity);
      }
  
      public void update(final T entity) {
          Preconditions.checkNotNull(entity);
          this.getCurrentSession().merge(entity);
      }
  
      public void delete(final T entity) {
          Preconditions.checkNotNull(entity);
          this.getCurrentSession().delete(entity);
      }
  
      public void deleteById(final Long entityId) {
          final T entity = this.getById(entityId);
          Preconditions.checkState(entity != null);
          this.delete(entity);
      }
  
      protected final Session getCurrentSession() {
          return this.sessionFactory.getCurrentSession();
      }
  }
  </code></pre>
  
  <p>When trying to create a new entity (last line of snippet) I get an error:</p>
  
  <pre><code>ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 
  
  ChemicalStructureDAO structureDAO = (ChemicalStructureDAO) context.getBean("ChemicalStructureDAO");
  
  ChemicalStructure structure1 = new ChemicalStructure();
  structure1.setStructureKey("c1ccccc1");
  structure1.setStructureData("c1ccccc1");
  
  structureDAO.create(structure1);
  </code></pre>
  
  <p>I'm getting a NullPointerException:</p>
  
  <pre><code>java.lang.NullPointerException
      at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.getCurrentSession(AbstractHibernateDAO.java:78)
      at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.create(AbstractHibernateDAO.java:54)
      at org.bitbucket.myName.moleculedatabaseframework.App.main(App.java:32)
  ------------------------------------------------------------------------
  </code></pre>
  
  <p>Maybe I missunderstood what autowired means? I thought that that property will be set automatically. So I tried following:</p>
  
  <pre><code>ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 
  SessionFactory sessionfactory = (SessionFactory)context.getBean("sessionFactory");
  
  ChemicalStructureDAO structureDAO = (ChemicalStructureDAO) context.getBean("ChemicalStructureDAO"); 
  structureDAO.setSessionFactory(sessionfactory);
  
  ChemicalStructure structure1 = new ChemicalStructure();
  structure1.setStructureKey("c1ccccc1");
  structure1.setStructureData("c1ccccc1");
  
  structureDAO.create(structure1);
  </code></pre>
  
  <p>This leads to following error:</p>
  
  <pre><code>Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread
  at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
  at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:941)
  at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.getCurrentSession(AbstractHibernateDAO.java:78)
  at org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO.create(AbstractHibernateDAO.java:54)
  </code></pre>
  
  <p>I looked at tons of tutorials but they all omit what seems the basic stuff to get things running, eg. ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");  does not appear in any spring + hibernate tutorials. Can someone point me at a complete tutorial one that assumes I'm completely dumb and tells me every step required and has an application that actually runs when repeating the code? (yes getting pretty frustrated now. To be honest if I went plain jdbc I would have been up and running hours ago)</p>
  
  <p>Now,how can I get this running? How does autowired work?</p>
  
  <p>EDIT:
  THE SOLUTION AS FOUND THROUGH THE HELP OF "Accepted Answer":</p>
  
  <p>The new Spring configuration file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
  
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  "&gt;      
  
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire="autodetect"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalStructure&lt;/value&gt;
                  &lt;value&gt;org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompound&lt;/value&gt;
                  &lt;value&gt;org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompoundComposition&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;     
  
      &lt;!-- Spring bean configuration. Tell Spring to bounce off BoneCP --&gt;
      &lt;bean id="dataSource"
            class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"&gt;
          &lt;property name="targetDataSource"&gt;
              &lt;ref local="mainDataSource" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- BoneCP configuration --&gt;
      &lt;bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"&gt;
          &lt;property name="driverClass" value="org.postgresql.Driver" /&gt;
          &lt;property name="jdbcUrl" value="jdbc:postgresql:MolDB" /&gt;
          &lt;property name="username" value="postgres"/&gt;
          &lt;property name="password" value="123456"/&gt;
          &lt;property name="idleConnectionTestPeriod" value="60"/&gt;
          &lt;property name="idleMaxAge" value="240"/&gt;      
          &lt;property name="maxConnectionsPerPartition" value="60"/&gt;
          &lt;property name="minConnectionsPerPartition" value="20"/&gt;
          &lt;property name="partitionCount" value="3"/&gt;
          &lt;property name="acquireIncrement" value="10"/&gt;                              
          &lt;property name="statementsCacheSize" value="50"/&gt;
          &lt;property name="releaseHelperThreads" value="3"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="txManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;bean id="AbstractHibernateDAO" abstract="true"
            class="org.bitbucket.myName.moleculedatabaseframework.dao.AbstractHibernateDAO"/&gt;
  
      &lt;bean id="ChemicalStructureDAO" parent="AbstractHibernateDAO"
            class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalStructureDAO"/&gt;
      &lt;bean id="ChemicalCompoundDAO" parent="AbstractHibernateDAO"
            class="org.bitbucket.myName.moleculedatabaseframework.dao.ChemicalCompoundDAO"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I had to add</p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  </code></pre>
  
  <p>to the file and declare the annoted entity classes in sessionFactory configuration:</p>
  
  <pre><code>&lt;property name="annotatedClasses"&gt;
      &lt;list&gt;
          &lt;value&gt;org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalStructure&lt;/value&gt;
          &lt;value&gt;org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompound&lt;/value&gt;
          &lt;value&gt;org.bitbucket.myName.moleculedatabaseframework.entityclasses.ChemicalCompoundComposition&lt;/value&gt;
      &lt;/list&gt;
  &lt;/property&gt;
  </code></pre>
  
  <p>The I had to uncomment the transaction Manager part and because of that change the data source configuration as the one I used did not work (DataSource is required).</p>
  
  <p>I also had to add</p>
  
  <pre><code>@Repository
  @Transactional
  public abstract class AbstractHibernateDAO&lt; T extends Serializable&gt; {
      //code...
  }
  </code></pre>
  
  <p>to AbstractHibernateDAO. I'm considering to write a blog post and make a link here. For anyone completley new to Spring and hibernate that would be very useful.</p>