Hibernate SessionFactory vs Spring LocalSessionFactoryBean

时间:2013-08-02 08:02:20

标签: spring hibernate

我正在尝试将spring3.2.x + hibernate4.x设置从xml移动到java配置 下面是现有代码的片段:

import org.hibernate.SessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;

public class StockDaoImpl implements StockDao{

    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;}
    public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
    }

    public void save(Stock stock){
        Session session = getSessionFactory().openSession();
        try{
        session.save(stock);
        }
        finally{
        session.close();
        }
    }

弹簧配置文件

<!-- Stock Data Access Object -->
   <bean id="stockDao" class="com.data.dao.StockDaoImpl" >
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean> 

    <bean id="dataSource">
.....
</bean>

    <bean id="sessionFactory" 
         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource">
          <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
           ......
         </property>
        <property name="mappingResources">
        .......
        </property> 
    </bean>

这很好用 - 但是如何在java中重新定义这个配置呢? 这是一次尝试 -

@Bean
    public StockDao stockDao() {
        StockDaoImpl dao=new StockDaoImpl();
        dao.setSessionFactory(sessionFactory());
        return dao;
    }

......
   @Bean
   public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
      sessionFactory.setDataSource(restDataSource());
      sessionFactory.setPackagesToScan(new String[] { .....});
      sessionFactory.setHibernateProperties(hibernateProperties());

      return sessionFactory;
   }

但是这不会编译,因为被引用的sessionFactory不是Spring的LocalSessionFactoryBean?

我该如何调和?

3 个答案:

答案 0 :(得分:12)

LocalSessionFactoryBean是FactoryBean<SessionFactory>。这意味着它允许创建SessionFactory的实例。

所以你需要

public StockDao stockDao() {
    StockDaoImpl dao=new StockDaoImpl();
    dao.setSessionFactory(sessionFactory().getObject());
    return dao;
}

但我不会像那样创建DAO explicitel。只需使用@Repository注释您的DAO,然后使用@Autowired自动装配会话工厂。确保Java配置类使用@ComponentScan("the.base.package.of.daos")

进行注释

答案 1 :(得分:0)

在回复时缺少getObject()调用 含return sessionFactory.getObject()的替代物(并改变返回类型!)

答案 2 :(得分:0)

JB Nizet's answer帮助了我,但我需要分别声明bean才能使其起作用:

@Bean
public LocalSessionFactoryBean sessionFactoryBean(final DataSource dataSource,
                                              final Properties hibernateProperties,
                                              final List<String> hibernateMappings) {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setHibernateProperties(hibernateProperties);
    sessionFactoryBean.setMappingResources(hibernateMappings.toArray(new String[hibernateMappings.size()]));
    return sessionFactoryBean;
}

@Bean
public SessionFactory sessionFactory(LocalSessionFactoryBean sessionFactoryBean) {
    return sessionFactoryBean.getObject();
}