当前位置:编程学习 > JAVA >>

Spring提供的一些ORM框架,但是为什么hibernate的不到sessionFactory(讨论)

在spring中使用annotion的方式注解一些业务,因此DAO层也使用annotion的方式,但是我又希望使用spring提供的XXXDaoSupport类。
   但是在实际中使用XXXDaoImpl extends hibernateDaoSupport 时,就会出现sessionFactory is required (sessionfactory是必须的)。而我使用IbatisDaoSupport的时候,就不会出现上面的问题。
  sessionFactory和datasource的创建没有问题(这个是前提)
1.HibernateDaoSupport

   //设置SessionFactory
   public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}

2.SqlMapClientDaoSupport(ibatis)

   private SqlMapClientTemplate sqlMapClientTemplate = new SqlMapClientTemplate();

private boolean externalTemplate = false;


/**
 * Set the JDBC DataSource to be used by this DAO.
 * Not required: The SqlMapClient might carry a shared DataSource.
 * @see #setSqlMapClient
 */
public final void setDataSource(DataSource dataSource) {
if (!this.externalTemplate) {
   this.sqlMapClientTemplate.setDataSource(dataSource);
}
}


为什么ibatis的SqlMapClientDaoSupport 就能注入DataSource 而hibernateDaoSupport就不能注入sessionFactory.
本题的主要目的就是为了讨论这个原因。hibernateDaoSupport得到sessionFactory的方式已经解决。希望大侠们能帮我解开这个问题!!!! --------------------编程问答-------------------- 用了spring去管理怎么有set,给个解释,本人新人 --------------------编程问答-------------------- 帮顶,使用初期,感觉还是XML用的习惯点。。。。 --------------------编程问答-------------------- hibernateDaoSupport也有setSessionFactory 这个方法的

hibernateDaoSupport


/**
 * Set the Hibernate SessionFactory to be used by this DAO.
 * Will automatically create a HibernateTemplate for the given SessionFactory.
 * @see #createHibernateTemplate
 * @see #setHibernateTemplate
 */
public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}

是你没看见吧 --------------------编程问答--------------------
引用 3 楼 jiangc198745 的回复:
hibernateDaoSupport也有setSessionFactory 这个方法的

hibernateDaoSupport
Java code


    /**
     * Set the Hibernate SessionFactory to be used by this DAO.
     * Will automatically create a HibernateT……



我晕。你提供得跟我提供的代码有区别吗?唯一的区别就是我加了一个注释。晕
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,