为什么不推荐HibernateDaoSupport? [英] Why is HibernateDaoSupport not recommended?
问题描述
HibernateDaoSupport
类使它变得简单和容易然而,在搜索一个不相关的问题时,我看到有人提到 HibernateDaoSupport
不是使用Spring和Hibernate的最佳方式。任何人都可以点亮: - 为什么不推荐?
- 什么是最好的(或至少被接受)的方式来集成Hibernate和Spring?
解决方案使用<$不建议使用HibernateDaoSupport /
HibernateTemplate
,因为它不必要地
将您的代码绑定到Spring类。
使用这些类对于旧版本的Hibernate来说是不可避免的,以便集成对Spring管理的事务的支持。然而,由于Hibernate 3.0 .1你不再需要它 - 你可以在使用Spring管理的事务时使用普通的Hibernate API编写代码。所有你需要的是配置Spring事务支持,当你需要工作时,注入
SessionFactory
并调用getCurrentSession()
与会议。
HibernateTemplate
的另一个好处是异常转换。如果没有HibernateTemplate
,可以使用@Repository
注释来实现相同的功能,如Gareth Davis的答案所示。
另见: ://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-hibernate-straightrel =noreferrer> 13.3.2基于纯Hibernate实现DAO 3 API
I've been doing some work with Hibernate 3.5 and Spring 3 recently, I'm fairly new with Hibernate and thought the HibernateDaoSupport
class in Spring made it nice and easy to use Hibernate with my domain classes.
However, while searching for an unrelated question I saw someone mention that the HibernateDaoSupport
is not the best way to use Spring and Hibernate. Can anyone shed any light on:
- Why is it not recommended?
- What is the best (or at least the accepted) way to integrate Hibernate and Spring?
Using HibernateDaoSupport
/HibernateTemplate
is not recommended since it unnecessarily
ties your code to Spring classes.
Using these classes was inevitable with older versions of Hibernate in order to integrate support of Spring-managed transactions.
However, since Hibernate 3.0.1 you don't need it any more - you can write a code against a plain Hibernate API while using Spring-managed transactions. All you need is to configure Spring transaction support, inject SessionFactory
and call getCurrentSession()
on it when you need to work with session.
Another benefit of HibernateTemplate
is exception translation. Without HibernateTemplate
the same functionality can be achieved by using @Repository
annotation, as shown in Gareth Davis's answer.
See also:
这篇关于为什么不推荐HibernateDaoSupport?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!