spring DAO(用hibernate)实现

 HibernateTemplate

对于特定的数据访问对象或业务对象的方法来说,基本的模板编程模型看起来像下面所示的代码那样。 对于这些外部对象来说,没有任何实现特定接口的要求,仅仅要求提供一个Hibernate SessionFactory。 它可以从任何地方得到,不过比较适宜的方法是从Spring的application context中得到的bean引用:通过简单的 setSessionFactory(..) 这个bean的setter方法。 下面的代码展示了在application context中一个DAO的定义,它引用了上面定义的 SessionFactory,同时展示了一个DAO方法的具体实现。

<beans>
  

  
<bean id="myProductDao" class="product.ProductDaoImpl">
    
<property name="sessionFactory" ref="mySessionFactory"/>
  
</bean>

</beans>

 

public class ProductDaoImpl implements ProductDao {

    
private SessionFactory sessionFactory;

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


    
public Collection loadProductsByCategory(final String category) throws DataAccessException {
        HibernateTemplate ht 
= new HibernateTemplate(this.sessionFactory);
        
return (Collection) ht.execute(new HibernateCallback() {
            
public Object doInHibernate(Session session) throws HibernateException {
                Query query 
= session.createQuery(
                    
"from test.Product product where product.category=?");
                query.setString(
0, category);
                
return query.list();
            }

        }
);
    }

}

一个回调实现能够有效地在任何Hibernate数据访问中使用。HibernateTemplate 会确保当前Hibernate的 Session 对象的正确打开和关闭,并直接参与到事务管理中去。 Template实例不仅是线程安全的,同时它也是可重用的。因而他们可以作为外部对象的实例变量而被持有。对于那些简单的诸如find、load、saveOrUpdate或者delete操作的调用,HibernateTemplate 提供可选择的快捷函数来替换这种回调的实现。 不仅如此,Spring还提供了一个简便的 HibernateDaoSupport 基类,这个类提供了 setSessionFactory(..) 方法来接受一个 SessionFactory 对象,同时提供了 getSessionFactory()getHibernateTemplate() 方法给子类使用。 综合了这些,对于那些典型的业务需求,就有了一个非常简单的DAO实现:

public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

    
public Collection loadProductsByCategory(String category) throws DataAccessException {
        
return getHibernateTemplate().find(
            
"from test.Product product where product.category=?", category);
    }

}

不使用回调的基于Spring的DAO实现

作为不使用Spring的 HibernateTemplate 来实现DAO的替代解决方案,你依然可以用传统的编程风格来编写你的数据访问代码。 无需将你的Hibernate访问代码包装在一个回调中,只需符合Spring的通用的 DataAccessException 异常体系。 Spring的 HibernateDaoSupport 基类提供了访问与当前事务绑定的 Session 对象的函数,因而能保证在这种情况下异常的正确转化。 类似的函数同样可以在 SessionFactoryUtils 类中找到,但他们以静态方法的形式出现。 值得注意的是,通常将一个false作为参数(表示是否允许创建)传递到 getSession(..) 方法中进行调用。 此时,整个调用将在同一个事务内完成(它的整个生命周期由事务控制,避免了关闭返回的 Session 的需要)。

 

public class ProductDaoImpl implements ProductDao {

    
private SessionFactory sessionFactory;

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


    
public Collection loadProductsByCategory(String category) {
        
return this.sessionFactory.getCurrentSession()
                .createQuery(
"from test.Product product where product.category=?")
                .setParameter(
0, category)
                .list();
    }

}


 

posted on 2007-09-18 21:14  灵风  阅读(1526)  评论(2编辑  收藏  举报