Hibernate Session/Template returns null
<p>I'm pretty new to Spring, and I probably miss something obvious here. I'm working on a project that dates back to 2008 or so, which uses Spring (v4.2.5) and Hibernate (v3.5.6). I've copied some code from another project of around the same time-period, and currently try to access the Hibernate Session from that copied code.</p>

<p>Some things I've tried, which all gave back <code>null</code> for both the <code>HibernateTemplate</code> and <code>HibernateSession</code>:</p>

<p>1.</p>

<pre><code>org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
  .getSession(getSessionFactory(), true);
// getSessionFactory comes from the Parent class 
//   org.springframework.orm.hibernate3.support.HibernateDaoSupport
</code></pre>

<p>2.</p>

<pre><code>org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
  .getSession(HibernateUtil.getSessionFactory(), true);

// Where HibernateUtil is our own factory-class:
import java.io.PrintStream;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
  private static final SessionFactory sessionFactory;

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  static {
    try {
      sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }
}
</code></pre>

<p>3.</p>

<pre><code>org.springframework.orm.hibernate3.HibernateTemplate hibernateTemplate = getHibernateTemplate();
org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
  .getSession(hibernateTemplate.getSessionFactory(), true);
// getHibernateTemplate comes from the Parent class 
//   org.springframework.orm.hibernate3.support.HibernateDaoSupport
</code></pre>

<p>4.</p>

<pre><code>org.springframework.orm.hibernate3.HibernateTemplate hibernateTemplate = getSpringHibernateTemplate();
org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
  .getSession(hibernateTemplate.getSessionFactory(), true);

// Where getSpringHibernateTemplate is:
@InjectObject("spring:hibernateTemplate")
public abstract HibernateTemplate getSpringHibernateTemplate();

// With the following bean in our ourprojectname-general.xml:
&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
    &lt;property name="mappingDirectoryLocations"&gt;
        &lt;!-- Add all Hibernate mappings to the list below --&gt;
        &lt;list&gt;
            &lt;value&gt;/WEB-INF/hbm&lt;/value&gt;
        &lt;/list&gt;
    &lt;/property&gt;
    &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
            &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt;
            &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
            &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;
            &lt;prop key="net.sf.ehcache.configurationResourceName"&gt;spinoff/dao/ehcache.xml&lt;/prop&gt;
            &lt;!-- Determines the size of the JDBC fetch, that is the maximum number 
                 of rows that are exchanged between Hibernate and the database in one go. 
                 It's been increased to reduce overhead when searching for all entiteits --&gt;
            &lt;prop key="hibernate.jdbc.fetch_size"&gt;500&lt;/prop&gt;
            &lt;prop key="hibernate.dialect"&gt;spinoff.objects.spatial.oracle.OracleSpatialDialect&lt;/prop&gt;
            &lt;prop key="hibernate.default_batch_fetch_size"&gt;25&lt;/prop&gt;
            &lt;prop key="hibernate.generate_statistics"&gt;false&lt;/prop&gt;
            &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
            &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
            &lt;prop key="hibernate.format_sql"&gt;false&lt;/prop&gt;
            &lt;prop key="use_sql_comments"&gt;false&lt;/prop&gt;
            &lt;prop key="hibernate.transaction.flush_before_completion"&gt;true&lt;/prop&gt;
            &lt;prop key="hibernate.connection.release_mode"&gt;after_transaction&lt;/prop&gt;
        &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;
&lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;
    &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
&lt;/bean&gt;
</code></pre>

<p>5.</p>

<p>I've also tried <a href="https://stackoverflow.com/a/4699592/1682559">this SO answer</a> with the same result:</p>

<blockquote>
  <p>java.lang.IllegalArgumentException: No SessionFactory specified
    at org.springframework.util.Assert.notNull(Assert.java:115)
    ...</p>
</blockquote>

<hr>

<p>On most other places in the code it's done like at <em>1</em>, and it was also done like this in the copied code in the other project.  </p>

<p>My guess is that I need to <code>@InjectObject</code> or <code>@AutoWired</code> a getter for the Spring-bean of either the HibernateTemplate or Session, something like I've tried at <em>4</em>, but it remains returning <code>null</code>.</p>

<p>Could anyone point me to an answer? All I want is a Hibernate DB-Session in my class. If you need to see the code of any other .java or .xml files let me know.</p>