3

I'm using Hibernate 3.3 and Spring 3.2 through java configuration. I would like to create a session factory in my application context. I'm trying something like this

@Bean
public SessionFactory sessionFactory() throws Exception {
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource());
    sessionFactoryBean.setPackagesToScan(new String[] { "org.package" });

    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.show_sql", true);
    hibernateProperties.put("hibernate.bytecode.use_reflection_optimizer", false);
    hibernateProperties.put("hibernate.check_nullability", false);
    hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.DB2Dialect");
    hibernateProperties.put("hibernate.search.autoregister_listeners", false);
    sessionFactoryBean.setHibernateProperties(hibernateProperties);

    return sessionFactoryBean.getObject();
}

But in the last line it returns null ¿What am I doing wrong?

1
  • I'm not sure if AnnotationSessionFactoryBean works if you directly create a new instance of it. Try instantiating it with dependency injection instead. Eg: mkyong.com/spring/… Commented Sep 7, 2013 at 4:59

1 Answer 1

3

Before returning the SesionFactory, you need to make a call to afterPropertiesSet:

sessionFactoryBean.afterPropertiesSet();

which builds and exposes the SessionFactory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.