Search code examples
springhibernatedependency-injectionspring-annotations

Spring Hibernate-sessionfactory is null while trying to inject it in DAO


The session factory I have defined is null in DAO. Here goes my code:

@Repository
public class LinkDetailsDAO {

     private SessionFactory sessionFactory;

     @Autowired
     public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
     }

     Session session = sessionFactory.getCurrentSession();

throws a NullPointerException when I am trying to create a session object.

My applicationContext:

  <!-- Load Hibernate related configuration -->
  <import resource="hibernate-context.xml"/>

 <context:annotation-config/>
 <context:component-scan base-package="com.Neuverd.*"/>

My hibernate-context:

 <context:property-placeholder location="/WEB-INF/config/testapp.properties" />

<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
 p:dataSource-ref="dataSource"
 p:configLocation="/WEB-INF/config/hibernate.cfg.xml"
 p:packagesToScan="com.Neuverd"/>

 <!-- Declare a datasource that has pooling capabilities--> 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
 destroy-method="close"
p:driverClassName="${app.jdbc.driverClassName}"
p:url="${app.jdbc.url}"
p:username="${app.jdbc.username}"
p:password="${app.jdbc.password}"
/>

and my hibernate config file

<hibernate-configuration>
    <session-factory>
        <!-- We're using MySQL database so the dialect needs to MySQL as well-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- Enable this to see the SQL statements in the logs-->
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
    </session-factory>
</hibernate-configuration>

I have tried using the @Resource annotation too. but no luck. I am using Spring 3.1 and Hibernate 4.1.

The application throws a BeanCreationException for LinkDetailsDAO during start-up, caused due to the above-mentioned NullPointerException.

After the sessionFactory bean and transactionManager bean is created, when the container tries to create the LinkDetailsDAO bean, it fails. I don't understand why a null sessionFactory bean is created!!. Tried a sessionFactory as mentioned in the spring doc. Not working.


Solution

  • You try to call sessionFactory.getCurrentSession() in the constructor. But the object has to be constructed first, before Spring can call the setter and inject the session factory. So obviously, at construction time, the session factory is null.

    Even if the session factory was injected in the constructor and you asked for a session after, there wouldn't be any transactional context, and getCurrentSession() would throw an exception.

    You should get a session from the factory only from inside the methods of the DAO. This is the way to get the current session, i.e. the session associated to the current transaction.

    public void doSomething() {
        Session session = sessionFactory.getCurrentSession();
        ...
    }