惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
J
Java Code Geeks
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
B
Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
美团技术团队
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
N
Netflix TechBlog - Medium
C
Check Point Blog
Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 叶小钗
T
Tailwind CSS Blog

博客园 - 灵风

python网站收集 在PHP中PDO解决中文乱码问题的一些补充 - 灵风 - 博客园 添加XP/2003的网络用户和密码及用户自动登录 一个命令行解决U盘所有文件(夹)被隐藏问题 - 灵风 - 博客园 Windows Server 2000/2003/2008错误 IntelliJ IDEA 性能调整 软件下载站 丁香园-英语面试精选 英语面试自我介绍范文(二) 英文面试自我介绍(一) 网络ghost mysql支持gbk字符集 Eclipse 3.2设置缩进设置 JSTL的问题(According to TLD or attribute directive in tag file, attribute value does not accept any expressions) 修改脱管(Detached)对象 金山词霸2005版词典文件对照表 1、WEB请求的大概过程 netbeans中设置字符集为 UTF8 取消XP/Windows 2003系统自带文件解压缩功能
spring DAO(用hibernate)实现
灵风 · 2007-09-18 · via 博客园 - 灵风

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

}