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

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

问题描述

在典型的现实生活场景中,人们会选择Spring Data JDBC/Spring Data JPA vs Hibernate吗?我想了解最适合这两种实现方式的情况.

解决方案

正如@Naros所说的那样,当前标题中的问题并没有真正起作用.似乎我们应该真正看一下4个选项,并且主要列出每种方法的优点,缺点是没有其他方法的优点:

没有Spring数据的JDBC

您可以对所发生的事情进行100%的细粒度控制.框架不会生成或注入任何内容.这听起来像是一个骗局,但是如果您尝试调整映射和配置以使某些JPA实现执行您可以用Java和SQL轻松编写的功能,那么您会理解这可能是个好主意.

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

  • 对域模型建模没有任何要求(例如,JPA需要默认构造函数)

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

  • JOOQ

  • MyBatis

  • Spring JdbcTemplate(在没有Spring其余部分的情况下可用)

  • QueryDsl

带有Spring数据的JDBC

您将获得Spring Data与JDBC相结合的好处(见上文)

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

  • Spring基础结构中很好的集成,用于事务处理,依赖项注入,错误翻译,分页...

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

虽然尚不可用,但未来的更新将为您带来更多好处:

  • 使用查询方法扩展存储库的好方法,方法很简单(只需将接口定义为具有findByLastName方法,Spring即可为您动态生成它)或@query批注或自定义方法. /p>

  • 支持分页

没有Spring Data的休眠(或其他JPA实现)

JPA通过JDBC做很多事情

  • 缓存(第一级,第二级和查询缓存)

  • 通过查询自动创建实例

  • 实体之间的导航

  • 延迟加载

随着所有这些事情的发生,可能很难理解正在发生的事情和原因.当然, IFF 可以正确地构建应用程序的结构,如果JPA无法提供所需的功能,则可以使用JDBC.但是我多次看到人们未能维持其正常工作所需的结构.显然,如果您不正确地理解JPA的工作原理,这将尤其困难.

使用Spring Data休眠(或其他一些JPA实现)

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

当然,这会使整个堆栈变得更加复杂.从用spring-data和hibernate标记的许多问题看来,许多开发人员在确定哪个工具执行功能时遇到问题.而且从这​​些问题来看,大多数描述的是Hibernate/JPA的问题,而不是Spring Data的问题.

总结一下:

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

  • 如果您要使用JPA,请确保您早点理解了.

  • 如果对于持久性技术,您选择的是Spring Data提供的模块,那么我将使用它.这将使生活更轻松.但是我再次有偏见.

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.

解决方案

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:

JDBC without Spring Data

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.

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.

  • no requirements how you model your domain model (JPA requires default constructors for example)

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

  • JOOQ

  • MyBatis

  • Spring JdbcTemplate (usable without the rest of Spring)

  • QueryDsl

JDBC with Spring Data

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

  • Repositories with CRUD methods out of the box.

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

  • 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.

While not available yet you'll get even more goodies with future updates:

  • 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.

  • Support for paging

Hibernate (or some other JPA implementation) without Spring Data

JPA does a lot of things over JDBC

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

  • Automated creation of instances from queries

  • Navigation between entities

  • Lazy loading

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.

Hibernate (or some other JPA implementation) with Spring Data

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

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.

To wrap it up:

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

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

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