How do you create a SessionFactory using the java config?
@Bean
public SessionFactory sessionFactory(){
AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
return sessionFactoryBean.getObject();
}
This doesnt work for some reason…it always returns null.
Worth noting here that Spring 3.1 introduces LocalSessionFactoryBuilder, which is expressly designed for use within @Bean methods.
http://static.springsource.org/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html
This gets around the awkward need to deal with FactoryBeans, getObject() methods, etc. FactoryBeans are excellent for use in XML, but non-ideal in @Bean methods.
Note that this new builder is Hibernate 4.1+ only.