新版本中spring与hibernate整合dao层调用session的方法

(选自:《Beginning Hibernate》及《spring-framework-3.0.5.RELEASE reference》)

在spring和hibernate整合框架中,session factory被配置为一个spring bean,以前版本会利用HibernateDaoSupport和HibernateTemplate这两个类,在新版本中可直接利用方法 session factory的getCurrentSession()返回当前事务的session。

如:


Hibernate 3.0.1开始具有有contextual sessions特性,这样hibernate为每个事务管理一个当前session。之前都是spring的相关支持类来实现这个功能。我们推荐使用这种方式。
优点:只依赖于hibernate,不再需要spring相关类。减小入侵性。
(Hibernate 3 has a feature called contextual sessions, wherein Hibernate itself manages one current Session per transaction. This is roughly equivalent to Spring's synchronization of one Hibernate Session per transaction. A corresponding DAO implementation resembles the following example, based on the plain Hibernate API:)
Spring的 LocalSessionFactoryBean为其事务策略 支持Hibernate的
SessionFactory.getCurrentSession() 方法, 返回当前spring管理的事务session,
(Spring's LocalSessionFactoryBean supports Hibernate's
SessionFactory.getCurrentSession() method for any Spring transaction strategy, returning
the current Spring-managed transactional Session even with HibernateTransactionManager.)



With your session factory configured as a Spring bean, you can now go on to create DAOs that take
advantage of Hibernate’s functionality. Previous versions of Spring and Hibernate required the use of
the HibernateDaoSupport class and/or HibernateTemplate class to form the basis of your DAOs; however,
recent versions of Spring and Hibernate have eliminated the need for these classes. Hibernate now
supports a getCurrentSession() method on the SessionFactory that returns a Session object that is
associated with the current transaction.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值