为什么不推荐 HibernateDaoSupport? [英] Why is HibernateDaoSupport not recommended?

查看:34
本文介绍了为什么不推荐 HibernateDaoSupport?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在用 Hibernate 3.5 和 Spring 3 做一些工作,我对 Hibernate 还很陌生,我认为 Spring 中的 HibernateDaoSupport 类使我的域使用 Hibernate 变得很好且容易类.

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.

然而,在搜索一个不相关的问题时,我看到有人提到 HibernateDaoSupport 不是使用 Spring 和 Hibernate 的最佳方式.谁能解释一下:

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:

  • 为什么不推荐?
  • 集成 Hibernate 和 Spring 的最佳(或至少是公认的)方式是什么?

推荐答案

不推荐使用 HibernateDaoSupport/HibernateTemplate 因为它是不必要的将您的代码与 Spring 类联系起来.

Using HibernateDaoSupport/HibernateTemplate is not recommended since it unnecessarily ties your code to Spring classes.

为了集成对 Spring 管理的事务的支持,旧版本的 Hibernate 不可避免地要使用这些类.

Using these classes was inevitable with older versions of Hibernate in order to integrate support of Spring-managed transactions.

但是,从 Hibernate 3.0.1 开始,您不再需要它了——您可以在使用 Spring 管理的事务时针对普通的 Hibernate API 编写代码.您只需要配置 Spring 事务支持,注入 SessionFactory 并在需要使用 session 时调用 getCurrentSession() 即可.

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.

HibernateTemplate 的另一个好处是异常翻译.如果没有 HibernateTemplate,可以通过使用 @Repository 注释来实现相同的功能,如 Gareth Davis 的回答所示.

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.

另见:

这篇关于为什么不推荐 HibernateDaoSupport?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆