Spring JDBCTemplate VS Hibernate 在性能方面

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

Spring JDBCTemplate VS Hibernate in terms of performance

springhibernatejdbctemplate

提问by Amruta

In our project we have to decide between Spring JDBCTemplate and Hibernate.

在我们的项目中,我们必须在 Spring JDBCTemplate 和 Hibernate 之间做出选择。

I want to know which is better in terms of performance and implementation and design.and how?

我想知道在性能和实现和设计方面哪个更好如何?

回答by JB Nizet

If you do all you can to make both implementations very fast, the JDBC template will probably be a bit faster, because it doesn't have the overhead that Hibernate has. But it will probably take much more time and lines of code to implement.

如果您尽一切努力使这两个实现都非常快,那么 JDBC 模板可能会快一点,因为它没有 Hibernate 的开销。但它可能需要更多的时间和代码行来实现。

Hibernate has its learning curve, and you have to understand what happens behind the scenes, when to use projections instead of returning entities, etc. But if you master it, you'll gain much time and have cleaner and simpler code than with a JDBC-based solution.

Hibernate 有其学习曲线,您必须了解幕后发生的事情,何时使用投影而不是返回实体等。但是如果您掌握了它,您将获得更多时间,并且比使用 JDBC 获得更清晰、更简单的代码基于的解决方案。

I would say that in 95% of the cases, Hibernate is fast enough, or even faster than non-optimized JDBC code. For the 5% left, nothing forbids you to use something else, like Spring-JDBC for example. Both solutions are not mutually exclusive.

我会说在 95% 的情况下,Hibernate 足够快,甚至比未优化的 JDBC 代码还要快。对于剩下的 5%,没有什么可以禁止您使用其他东西,例如 Spring-JDBC。两种解决方案并不相互排斥。

回答by Aaron Digulla

That depends on your project and how well the Hibernate model fits the way you think. Speed/performance is irrelevant: If you can't wrap your mind about how Hibernate works, your project will be riddled with strange bugs that will take ages to find and fix.

这取决于您的项目以及 Hibernate 模型与您的思维方式的契合程度。速度/性能无关紧要:如果您无法完全了解 Hibernate 的工作原理,那么您的项目将充满奇怪的错误,这些错误需要很长时间才能找到并修复。

Also note that the internals of Hibernate will leak into your model and DAOs. Notable points of conflict are usually equals()/hashCode()and lazy loading of collections outside of transactions. Since the examples with Hibernate are so simple and you can achieve a lot in a short time, this can lead to the misconception that Hibernate is simple. It's not. Hibernate makes a lot of assumptions and forces you to think and code in a certain way.

另请注意,Hibernate 的内部结构会泄漏到您的模型和 DAO 中。值得注意的冲突点通常equals()/hashCode()是在事务之外延迟加载集合。由于 Hibernate 的示例非常简单,您可以在短时间内实现很多目标,因此可能会导致人们误认为 Hibernate 很简单。它不是。Hibernate 做出了很多假设,并迫使您以某种方式思考和编码。

Using JdbcTemplateis easier because it's just a very thin wrapper around JDBC itself. The price here is that you will write thousands of lines of really boring code. Also, you will find that SQL strings are really hard to maintain. When your data model changes, you will have to search your whole code base for all places which might be affected. It won't be pretty.

使用JdbcTemplate更容易,因为它只是围绕 JDBC 本身的一个非常薄的包装器。这里的代价是您将编写数千行非常无聊的代码。此外,您会发现 SQL 字符串真的很难维护。当您的数据模型发生变化时,您将不得不在整个代码库中搜索可能受到影响的所有地方。不会好看

For our own project, we decided against Hibernate because we have really complex data structures (revisioned tree structures) and have to build complex search queries at runtime. Instead, we wrote our own DAO layer using jOOQ. jOOQ is a thin wrapper around JDBC which allows you to write SQL with a nice DSL in Java:

对于我们自己的项目,我们决定不使用 Hibernate,因为我们有非常复杂的数据结构(修订后的树结构),并且必须在运行时构建复杂的搜索查询。相反,我们使用jOOQ编写了我们自己的 DAO 层。jOOQ 是一个围绕 JDBC 的瘦包装器,它允许您使用 Java 中的 DSL 编写 SQL:

create.selectFrom(BOOK)
      .where(PUBLISHED_IN.equal(2011))
      .orderBy(TITLE)

Like Hibernate, jOOQ has some rules which you should follow or you won't be happy but these are much more lenient.

像 Hibernate 一样,jOOQ 有一些你应该遵循的规则,否则你会不高兴,但这些规则要宽松得多。

As another option, you should have a look at Spring Data. In a nutshell, Spring Data allows you to store your data into anything that remotely resembles a database. This means you can manage your model with Hibernate and another using a NoSQL database. Or you can easily migrate part of your model as needed.

作为另一种选择,您应该看看Spring Data。简而言之,Spring Data 允许您将数据存储到远程类似于数据库的任何内容中。这意味着您可以使用 Hibernate 管理您的模型,另一个使用 NoSQL 数据库。或者,您可以根据需要轻松迁移部分模型。

One of the key features is that the DAO implementationlook like so:

关键特性之一是 DAO实现如下所示:

public interface UserRepository extends Repository<User, Long> {

  List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
}

Now you may wonder where the implementation is since this is just an interface with a method definition but that's it. At runtime, Spring Data will generate code for you which implements this method. This is possible because 99% of all the queries which you will need are of the form "query table for all rows where column X is ..." so they optimized this use case.

现在您可能想知道实现在哪里,因为这只是一个带有方法定义的接口,仅此而已。在运行时,Spring Data 将为您生成实现此方法的代码。这是可能的,因为您需要的所有查询中有 99% 的形式为“查询 X 列所在的所有行的表”,因此他们优化了这个用例。

OTOH, if you already know that you're going to build really complex search queries at runtime, Spring Data probably won't be of much help.

OTOH,如果您已经知道将在运行时构建非常复杂的搜索查询,那么 Spring Data 可能不会有太大帮助。

回答by Nandkumar Tekale

In our project, we are using both, JdbcTemplateand Hibernate. What you need to do is share DataSourcebetween hibernateand jdbcTemplate. We can check performance for both according to operations, whichever is better, we use better one. Mostly we are using hibernate for normal operations, if there are big queries or heavy operations, we check performance for jdbc and hibernate whichever better we use it.

在我们的项目中,我们同时使用,JdbcTemplateHibernate。您需要做的是DataSourcehibernate和之间共享jdbcTemplate。我们可以根据操作检查两者的性能,以更好的为准,我们使用更好的一个。大多数情况下,我们使用 hibernate 进行正常操作,如果有大查询或繁重操作,我们会检查 jdbc 和 hibernate 的性能,以我们使用的更好。

The good point is HibernateTransactionManagerworks for both (JdbcTemplate, plain jdbc) and hibernate.

好的一点是HibernateTransactionManager适用于(JdbcTemplate,plain jdbc)和休眠。

回答by Pasha Utt

Is your database design hibernate friendly? If yes then use hibernate...if not then you may want to avoid it. Jdbctemplate has many upsides and there are ways to make sure your SQL queries are easily maintained. Have a class that holds all of them or read them from a file etc. If columns have to be update there is a way to use standard jdbc to get resultset meta data allowing you to retrieve column names. This can be complex but an interesting way to solve an issue. Hibernate is a great tool but complex data models make it get really tricky.

您的数据库设计对休眠友好吗?如果是,则使用休眠……如果不是,则您可能希望避免使用它。Jdbctemplate 有很多优点,并且有很多方法可以确保您的 SQL 查询易于维护。有一个类来保存所有这些或从文件中读取它们等。如果必须更新列,有一种方法可以使用标准 jdbc 来获取结果集元数据,允许您检索列名。这可能很复杂,但却是解决问题的有趣方式。Hibernate 是一个很棒的工具,但是复杂的数据模型使它变得非常棘手。