Spring Hibernate SessionFactory [英] Spring Hibernate SessionFactory

查看:83
本文介绍了Spring Hibernate SessionFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Bean 
public SessionFactory sessionFactory(){
AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
sessionFactoryBean.setConfigLocation(new ClassPathResource(hibernate.cfg.xml));
return sessionFactoryBean.getObject();
}

由于某种原因,这不起作用......它总是返回空值。 p>

解决方案

值得注意的是,Spring 3.1引入了LocalSessionFactoryBuilder,它明确地用于在@Bean方法中使用。 b

http://static.springsource.org/spring/docs/3.1.0.RC1/javadoc-api/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.html



这解决了处理FactoryBeans,getObject()方法等尴尬的需求。FactoryBeans非常适合用于XML,但在@Bean方法中非常理想。



请注意,这个新的构建器只有Hibernate 4.1+。


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.

这篇关于Spring Hibernate SessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆