Java Spring Hibernate 模板何时使用以及为什么使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4067775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:13:35  来源:igfitidea点击:

Spring hibernate template when to use and why?

javahibernatespring

提问by artjomka

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:

问候,目前正在开发小型 Web 服务应用程序,其中来自 Web 服务(使用 CXF + Spring)的响应处理并保存到数据库中。为了使用数据库,我正在使用 Hibernate(3.5)。网上浏览了一些Hibernate + Spring的例子,经常可以看到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 ?

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

Thanks.

谢谢。

采纳答案by Sean Patrick Floyd

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

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

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 HibernateTemplateno longer be used.

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

Specifically, what HibernateTemplatedid 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.

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

Reference:

参考:



Update:

更新:

As of Spring 3.1 (and newer versions), HibernateTemplatehas been removed. See Hibernatefor the currently suggested usage patterns.

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

回答by duffymo

HibernateTemplate encapsulates a number of things for you to make your life easier.

HibernateTemplate 为您封装了许多东西,使您的生活更轻松。

It's your choice to use it or not. For that matter, you can work with a database without Hibernate. Spring's JDBC stuff is very good. You might find it easier to get your problem done without having to learn Hibernate.

使用与否是您的选择。就此而言,您可以在没有 Hibernate 的情况下使用数据库。Spring 的 JDBC 东西非常好。您可能会发现无需学习 Hibernate 即可更轻松地完成问题。

回答by Maulik Kayastha

Let me clarify one thing that Spring's HibernateTemplate will not be supported going forward, that means Hibernate 4+ versions do not support HibernateTemplate. So it is advised to use declarative transaction managementas suggested by Sean.

让我澄清一件事,即今后将不支持 Spring 的 HibernateTemplate,这意味着 Hibernate 4+ 版本不支持 HibernateTemplate。所以建议使用Sean 建议的声明式事务管理

回答by Thomas W

The OpenSessionInViewFilter pattern is effective. This opens a Hibernate session & binds it to your thread, during the processing of every request. OpenSessionInView also extends the Session and loadability to View rendering & the View layer, which decreases coupling & complexity (by enabling that to 'just work').

OpenSessionInViewFilter 模式是有效的。在处理每个请求期间,这会打开一个 Hibernate 会话并将其绑定到您的线程。OpenSessionInView 还将会话和可加载性扩展到视图渲染和视图层,这降低了耦合和复杂性(通过使其“正常工作”)。

My philosophies don't really agree with aspect-based/ declarative transaction management. I like to make major state-change/ lifecycle events 'explicit', since they should be absolutely definite -- not weakly dependent on multiple hidden & indirect layers, which may or may not work.

我的哲学并不真正同意基于方面/声明式的事务管理。我喜欢使主要的状态变化/生命周期事件“显式”,因为它们应该是绝对确定的——而不是弱依赖于多个隐藏层和间接层,这些层可能有效也可能无效。

It provides a point to debug at.

它提供了一个调试点。

TX commit is only one line of code; but it's the major one you want to breakpoint on. No longer syntactically, than a 'transactional' declaration; but a hell of a lot more definite.

TX commit只有一行代码;但它是您想要断点的主要内容。不再是句法上的,而是“事务性”声明;但要明确得多。

Frankly I find "user commands" or "requests", which are the proper place to initiate a transaction & control transactionality from, should be well-structured, well-identified & fairly explicit within the application.

坦率地说,我发现“用户命令”或“请求”是启动事务和控制事务的正确位置,它们应该在应用程序中结构良好、识别良好且相当明确。

(I did have trouble getting the aspect class-loading stuff to work, trying it when it first came out. My assessment is that compared to well-written OO code, aspect has only limited marginal value.)

(我确实在使方面类加载的东西工作时遇到了麻烦,在它第一次出现时尝试了它。我的评估是,与编写良好的 OO 代码相比,方面的边际价值有限。)

Tip: I generally make a helper class, to make it reallyconvenient to get the Session & to commit the Transaction.

提示:我通常会创建一个辅助类,以便真正方便地获取会话和提交事务。

HbHelper or somesuch.

HbHelper 之类的。

回答by Sandeep Rao Annamaneni

All the templates will be deprecated going forward. Better to use entity manager which is JPA's standard.

所有模板将被弃用。最好使用 JPA 标准的实体管理器。