Spring Data JDBC/Spring Data JPA 与 Hibernate [英] Spring Data JDBC / Spring Data JPA vs Hibernate

查看:50
本文介绍了Spring Data JDBC/Spring Data JPA 与 Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择 Spring Data JDBC/Spring Data JPA 与 Hibernate 的典型现实生活场景是什么?我想了解最适合这些实现的场景.

What are the typical real life scenarios where one would choose Spring Data JDBC / Spring Data JPA vs Hibernate? I would like to understand the scenarios where either of these implementations are best suited.

推荐答案

正如@Naros 所说,目前在标题中的问题并不真正有效.看来我们真的应该看看 4 个选项,并且主要列出每种方法的优点,缺点是没有其他方法的优点:

As @Naros said, the question as it is currently in the title doesn't really work. It seems we should really look at 4 options and mostly list the pros of each approach, the cons are the absence of the pros of the other:

没有 Spring 数据的 JDBC

您可以 100% 精细地控制正在发生的事情.框架不会生成或注入任何内容.这听起来可能是个骗局,但如果您尝试调整映射和配置以获得一些 JPA 实现来完成您可以用 Java 和 SQL 轻松写下的内容,您就会明白这可能是一个很大的优势.

You get 100% fine-grained control over what is happening. Nothing gets generated or injected by a framework. This might sound like a con, but if you have tried to tweak mappings and configurations to get some JPA implementation to do what you could trivially write down in java and SQL, you'll understand that this can be a big pro.

您不必学习 JPA,也不必学习 Spring Data.我个人认为 Spring Data 很容易,但我有偏见(请参阅我的个人资料).但是 JPA 无疑是具有挑战性的,一旦您离开了琐碎的实体和设置领域.

You don't have to learn JPA, nor Spring Data. I personally think Spring Data is easy, but I'm biased (see my Profile). But JPA is most certainly challenging, once you leave the area of trivial entities and setup.

  • 不要求您如何建模域模型(例如,JPA 需要默认构造函数)

您可能想使用一些库来减少样板代码.看看:

You probably want to use some library in order to cut down on boilerplate code. Take a look at:

  • JOOQ

  • JOOQ

MyBatis

Spring JdbcTemplate(无需 Spring 其余部分即可使用)

Spring JdbcTemplate (usable without the rest of Spring)

QueryDsl

JDBC 与 Spring 数据

您可以获得 Spring Data 的好处,结合 JDBC 的好处(见上文):

You get the benefits of Spring Data, combined with those of JDBC (see above):

  • 带有开箱即用 CRUD 方法的存储库.

  • Repositories with CRUD methods out of the box.

支持聚合.见 https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates

与 Spring 基础架构的良好集成,用于事务处理、依赖注入、错误翻译、分页......

Nice integration in the Spring infrastructure, for transaction handling, dependency injection, error translation, paging ...

它仍然是一个非常简单的编程模型.SQL 语句恰好在人们期望它们发生的时候发生,如果您愿意,您可以在有或没有其他框架支持的情况下退回到简单的 JDBC,而不会破坏任何抽象.

It is still a really simple programming model. SQL statements happen exactly when one would expect them to happen and if you want to you can fall back to simple JDBC with or without the support of other frameworks, without breaking any abstraction.

使用查询方法扩展存储库的好方法(您只需将接口定义为具有 findByLastName 方法,Spring 会即时为您生成它)或 @查询注解或自定义方法.

Nice and easy ways to extend your repositories with query methods (you just define your interface to have a findByLastName method and Spring generates it for you on the fly) or @Query annotations or custom methods.

支持分页

没有 Spring Data 的 Hibernate(或其他一些 JPA 实现)

JPA 在 JDBC 上做了很多事情

JPA does a lot of things over JDBC

  • 缓存(一级、二级和查询缓存)

  • Caching (1st, 2nd level, and query cache)

根据查询自动创建实例

实体间导航

延迟加载

脏检查/跟踪实体更改

随着所有这些事情的发生,可能很难理解正在发生的事情及其原因.当然,IFF 您可以正确构建您的应用程序,如果 JPA 没有提供您想要的东西,您可以只依靠 JDBC.但是我已经多次看到人们未能维护其工作所需的结构.显然,如果您不能正确理解 JPA 的工作原理,这将特别困难.

With all this stuff happening it can be difficult understanding what is happening and why. Of course IFF you structure your application properly, you can just fall back on JDBC if JPA doesn't offer what you want. But I have seen it multiple times that people failed to maintain the structure required for that to work. Obviously, this is especially difficult if you don't understand properly how JPA works.

使用 Spring Data 进行 Hibernate(或其他一些 JPA 实现)

我在上面列出了 Spring Data 的好处,只需进行心理复制和粘贴.

I listed the benefits of Spring Data above, just perform a mental copy&paste.

当然,这使得完整的堆栈更加复杂.来自许多标记为 似乎许多开发人员都难以识别哪个工具做什么.但从这些问题来看,最能描述 Hibernate/JPA 而不是 Spring Data 的问题.

Of course, this makes the complete stack even more complex. From the many questions tagged with spring-data AND hibernate it seems many developers have problems identifying which tool does what. But also from looking at these questions most describe problems with Hibernate/JPA and not Spring Data.

总结:

  • 如果您想要/需要细粒度控制,请使用 JDBC.

  • If you want/need fine-grained control use JDBC.

如果您打算使用 JPA,请确保您尽早了解它.

If you're going to use JPA, make sure you understand it early on.

如果您选择的持久性技术 Spring Data 提供了一个模块,我会使用它.它会让生活更轻松.但我还是有偏见.

If for the persistence technology you are choosing Spring Data offers a module, I would use it. It will make life easier. But again I'm biased.

这篇关于Spring Data JDBC/Spring Data JPA 与 Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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