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

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

问题描述

您好,目前正在开发小型 Web 服务应用程序,其中来自 Web 服务(使用 CXF + Spring)的响应处理并保存到数据库中.为了使用数据库,我正在使用 Hibernate(3.5).网上浏览了一些Hibernate + Spring的例子,经常可以看到HibernateTemplate的用法,所以此时有点迷茫,想问一下:

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:

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

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 ?

谢谢.

推荐答案

所有 spring 模板(hibernate、jdbc、rest、jpa 等)都有相同的优缺点:

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.

缺点:您的应用程序与 spring 框架紧密耦合.为此,Spring 建议不再使用 HibernateTemplate.

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

具体来说,HibernateTemplate 为您所做的是在您的代码执行后自动打开和关闭会话以及提交或回滚事务.但是,所有这些都可以使用 Spring 的 声明式事务管理.

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.

参考:

  • Spring Reference: ORM: Hibernate for the current suggested Spring Hibernate usage patterns
  • Spring Reference: Classic Spring Usage: HibernateTemplate

更新:

从 Spring 3.1(和更新版本)开始,HibernateTemplate 已经删除.参见 Hibernate当前建议的使用模式.

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天全站免登陆