Spring Java配置中的Hibernate 3 sessionFactory

时间:2013-09-06 22:23:23

标签: java spring hibernate

我正在通过java配置使用Hibernate 3.3和Spring 3.2。我想在我的应用程序上下文中创建一个会话工厂。我正在尝试这样的事情

@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();
}

但是在最后一行它返回null¿我做错了什么?

1 个答案:

答案 0 :(得分:3)

在返回SesionFactory之前,您需要拨打afterPropertiesSet

sessionFactoryBean.afterPropertiesSet();

构建并公开SessionFactory