Java 春季与休眠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/529904/
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-11 15:51:51  来源:igfitidea点击:

Spring vs Hibernate

javahibernatespring

提问by Vidar

Just trying to get my head round Spring and figuring out how I wire up an Oracle connection in xml config file, and now find out I need yet another framework! - Hibernate, this is soooo frustrating as it feels like I'm getting deeper and deeper into more and more frameworks without actually getting what I need done!

只是想了解一下 Spring 并弄清楚如何在 xml 配置文件中连接 Oracle 连接,现在发现我还需要另一个框架!- Hibernate,这太令人沮丧了,因为感觉我对越来越多的框架越来越深入,而实际上没有完成我需要做的事情!

I looked at Hibernate and it seems to do similar things to Spring, bearing in mind I just want to do some SQL inserts in Oracle.

我查看了 Hibernate,它似乎与 Spring 做类似的事情,记住我只想在 Oracle 中做一些 SQL 插入。

I am reluctant and do not have time to learn 2 frameworks - could I get away with just adopting Hibernate for the simple things I need to do?

我很不情愿,也没有时间学习 2 个框架 - 我可以只采用 Hibernate 来完成我需要做的简单事情吗?

采纳答案by OscarRyz

...could I get away with just adopting Hibernate for the simple things I need to do?

...我可以只采用 Hibernate 来完成我需要做的简单事情吗?

Yes

是的

Hibernate is for ORM ( object relational mapping ) that is, make your objects persistent to a RDBMS.

Hibernate 用于 ORM(对象关系映射),也就是说,使您的对象持久化到 RDBMS。

Spring goes further. It may be used also as a AOP, Dependency Injector, a Web Application and ORM among other things.

春天走得更远。它也可以用作 AOP、依赖注入器、Web 应用程序和 ORM 等。

So if you only need ORM, just use Hibernate. Time will come when you need Spring, and you will learn it then.

所以如果你只需要 ORM,就使用 Hibernate。需要 Spring 的时候会到来,到时候你就会学会它。

Here's an architectural view of Spring:

这是 Spring 的架构视图:

spring

春天

And this is Hibernate:

这是休眠状态:

hibernate

休眠

回答by André Boonzaaijer

Spring and Hibernate are totally different frameworks for different problems. Spring is a huge framework with many many features, Hibernate is an O/R bridge.

Spring 和 Hibernate 是针对不同问题的完全不同的框架。Spring 是一个巨大的框架,具有许多功能,Hibernate 是一个 O/R 桥梁。

I would recommend using plain old JDBC in your case ('just some SQL inserts in Oracle').

我建议在您的情况下使用普通的旧 JDBC(“只是 Oracle 中的一些 SQL 插入”)。

回答by Jeffrey Hantin

Spring and Hibernate are really intended to do two different things. Spring is first and foremost an inversion-of-control container and configuration subsystem, while Hibernate is a database binding and lazy loading engine. If you don't want to introduce a bunch of new stuff into your code, stick with Spring and roll your own queries or use iBatisto do much simpler database binding.

Spring 和 Hibernate 确实打算做两件不同的事情。Spring 首先是一个控制反转容器和配置子系统,而 Hibernate 是一个数据库绑定和延迟加载引擎。如果您不想在代码中引入一堆新东西,请坚持使用 Spring 并滚动您自己的查询或使用iBatis进行更简单的数据库绑定。

回答by krosenvold

You could get away with using just spring and spring-JDBC integration. Depending on the complexity of your data-access needs it may be more than enough. The spring Object-relation mappingis also worth looking into if you're going to do a lot of data-access.

您可以只使用 spring 和spring-JDBC 集成。根据您的数据访问需求的复杂性,它可能绰绰有余。如果您要进行大量数据访问,spring对象关系映射也值得研究。

The nice thing about spring is that it's a very loosely coupled framework. So you can read up on the bits you use, and forget the rest - even in the runtime.

Spring 的好处在于它是一个非常松散耦合的框架。所以你可以阅读你使用的位,而忘记其余的 - 即使在运行时也是如此。

回答by Jim Pauley

If all you want is insert sql for oracle I would stick to a simple JDBC library. All you need is a Connection and maybe some ConnectionPool (maybe c3po). Hibernate and the like are too big/too complicated and IMO inferior. Hibernate incorporates JDBC under the hood but in every measurable way is inferior -- harder to use, not faster, and the queries you have to write or not any easier. It is also a testament to their inferiority because HQL also provides a bypass route so you can enter JDBC queries directly. They provide this (I suspect) because for any complex query you simply can't construct it well in HQL.

如果你想要的只是为 oracle 插入 sql,我会坚持使用一个简单的 JDBC 库。您所需要的只是一个连接,也许还有一些 ConnectionPool(也许是 c3po)。Hibernate 之类的东西太大/太复杂,IMO 劣等。Hibernate 在幕后合并了 JDBC,但在每一个可衡量的方式上都比较差——更难使用,不是更快,并且您必须编写的查询或不更容易。这也证明了它们的自卑,因为 HQL 还提供了旁路路由,因此您可以直接输入 JDBC 查询。他们提供了这个(我怀疑),因为对于任何复杂的查询,您根本无法在 HQL 中很好地构建它。