Spring何时使用hibernate模板以及为什么? [英] Spring hibernate template when to use and why?

查看:96
本文介绍了Spring何时使用hibernate模板以及为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候语,
目前正在开发小型Web服务应用程序,其中来自Web服务(使用CXF + Spring)的响应已处理并保存到数据库中。为了使用数据库,我使用Hibernate(3.5)。在网上浏览一些Hibernate + Spring的例子,我经常可以看到HibernateTemplate的用法,所以我对这一刻有些困惑,并想问一下:

你使用HibernateTemplate在你的Hibernate3应用程序中?
什么时候HibernateTemplate可以让你的开发生活变得更好,并且基于我可以决定的要点,我是否需要使用它?



谢谢。

解决方案

所有Spring模板(hibernate,jdbc,rest,jpa等)都有相同的优点和缺点:

Pro:他们为您执行常用的安装例程,让您跳过样板并专注于您想要的逻辑。

Con:你将应用程序紧密地连接到spring框架。因此,Spring建议不再使用 HibernateTemplate



具体而言, HibernateTemplate 为你做的是在代码执行后自动打开和关闭会话并提交或回滚事务。但是,所有这些都可以通过Spring的声明式事务管理



参考: b






更新: b

从Spring 3.1(及更新版本)开始, HibernateTemplate 已被删除。请参阅 Hibernate for当前建议的使用模式。

Greetings, Currently developing small web service application where response from web service (using CXF + Spring) processed and saved to database. To work with database I am using Hibernate(3.5). Browsing some Hibernate + Spring example on web, I often can see the usage of HibernateTemplate so I am a bit confused about this moment and wanted to ask:

Do you use HibernateTemplate in your Hibernate3 applications? When does HibernateTemplate can make your development life better and based on what points can I decide do I need to use it or not ?

Thanks.

解决方案

All spring templates (hibernate, jdbc, rest, jpa etc.) have the same pros and cons:

Pro: They perform common setup routines for you, let you skip the boilerplate and concentrate on the logic you want.

Con: you are coupling your application tightly to the spring framework. For this reason, Spring recommends that HibernateTemplate no longer be used.

Specifically, what HibernateTemplate did for you was to automatically open and close sessions and commit or rollback transactions after your code executed. However, all of this can be achieved in an aspect-oriented way using Spring's Declarative Transaction Management.

Reference:


Update:

As of Spring 3.1 (and newer versions), HibernateTemplate has been removed. See Hibernate for the currently suggested usage patterns.

这篇关于Spring何时使用hibernate模板以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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