title: Spring + Hibernate + DAO + SessionFactory + Inject
body: <p>I use Spring with Hibernate and get always a NPE for the sessionFactory Object.</p>
  
  <p>My config file: </p>
  
  <pre><code>@Configuration
  public class HibernateConfiguration {
  
  @Bean
  public AnnotationSessionFactoryBean sessionFactory() {
      Properties props = new Properties();
      props.put("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
      props.put("hibernate.format_sql", "true");
      props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
      props.put("hibernate.connection.password", "xxx");
      props.put("hibernate.connection.url", "jdbc:mysql://localhost/Market");
      props.put("hibernate.connection.username", "philipp");
  
      AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
      bean.setAnnotatedClasses(new Class[] { xxx.class, xxx.class, xxx.class });
      bean.setHibernateProperties(props);
      bean.setSchemaUpdate(true);
      return bean;
  }
  
  
  
  
  
  @Bean
  public HibernateTransactionManager transactionManager() {
      return new HibernateTransactionManager(sessionFactory().getObject());
  }
  
  @Bean
  public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
      return new PersistenceExceptionTranslationPostProcessor();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My DAOImpl class:</p>
  
  <pre><code>@Repository("xxx")
  public class xxxDAOImpl implements xxxDAO {
  
  private SessionFactory sessionFactory;
  
  @Autowired
  public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
  }
  
  private Session currentSession() {
      return sessionFactory.getCurrentSession();
  }
  ...
  </code></pre>
  
  <p>Testcase: </p>
  
  <pre><code>@ContextConfiguration
  @RunWith(SpringJUnit4ClassRunner.class)
  public class TestxxxDAOImpl {
  
  @Test
  @Transactional
  public void testInsertxxx() throws Exception {
  
      xxxDAO xxxDAO = new xxxDAOImpl();
      xyz xyz = new xyz();
      xxxDAO.insert(xyz);
      assertNotNull(xyz.getId());
      }
  </code></pre>
  
  <p>}</p>
  
  <p>app-context.xml</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:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  xsi:schemaLocation="
      http://www.springframework.org/schema/jdbc   http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
      http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  &lt;context:component-scan base-package="xxx.config" /&gt;
  &lt;context:component-scan base-package="xxx.dao" /&gt;
  &lt;context:annotation-config&gt;&lt;/context:annotation-config&gt;
  </code></pre>
  
  <p></p>
  
  <p>test-context.xml</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:jdbc="http://www.springframework.org/schema/jdbc"
  xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
  &lt;import resource="classpath:/META-INF/spring/app-context.xml" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>I get always an NPE from the currentSession when the test calls the insert method.</p>
  
  <pre><code>public void insert(xxx xxx) {
      currentSession().save(xxx);
  }
  
  
  private Session currentSession() {
      return sessionFactory.getCurrentSession();
  }
  </code></pre>