本文介绍了ReactiveCrudRepository在春季使用Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Hibernate和RDBMS(Mysql,Postgres等)与ReactiveCrudRepository而不是CrudRepository一起使用?我已经使用Spring Data Jpa和Hibernate尝试了一些示例,但是无法完成.我只能在ReactiveCrudRepository上找到MongoDB和 cassandra 上的一些示例.

Is it possible to use Hibernate and RDBMS(Mysql, Postgres etc) with ReactiveCrudRepository instead of CrudRepository? I have tried some samples with Spring Data Jpa and Hibernate, but couldn't get it done. I was only able to find a few samples on ReactiveCrudRepository for MongoDB and cassandra.

推荐答案

TL; DR:

不是使用Hibernate和MySQL,而是使用 R2DBC 和Postgres,Microsoft SQL Server或H2.看看 Spring Data R2DBC .

TL;DR:

Not with Hibernate and MySQL, but with R2DBC and Postgres, Microsoft SQL Server or H2.Take a look at Spring Data R2DBC.

随着Hibernate/JPA的加入,这种情况在可预见的将来不会发生.JPA基于这样的想法:将部分数据模型加载到内存中,操纵结果对象模型,然后让JPA转换这些更改.所有这些都在一次交易中.

With Hibernate/JPA included this won't happen in the foreseeable future.JPA is based on the idea that you load part of your data model into memory, manipulate the resulting object model and let JPA transform these changes.All this within a single transaction.

这与处理被动式存储的方式正好相反,在被动式存储中,您尝试进行原子更改,并尝试将加载,处理和存储解耦,而所有这些都不会阻塞.

This is kind of the opposite how one deals with a reactive store where you try to make atomic changes and try to decouple the loading, processing and storing and all this without blocking.

因此,我们必须看一下JPA以下的技术级别:JDBC.但是JDBC仍然处于阻塞状态:您将SQL语句发送到数据库,然后JDBC将阻塞直到获得结果.这再次违背了反应式的思想:永不阻塞.可以将其包装在线程池中以在某种程度上减轻这种情况,但这比解决方案更多是一种解决方法.

So we have to look at the technology level below JPA: JDBC.But JDBC is still blocking: You send a SQL statement to your database and then JDBC will block until you get the result. And again this goes against the idea of reactive: Never block. One could wrap this in a thread pool to mitigate this to some extent, but that is more of a workaround than a solution.

对于某些可用于反应性存储库的数据库,有一些合适的驱动程序.但是它们是专有的,因此并不是最终应在所有(相关)关系数据库中都能使用的东西的良好基础.

There are some suitable drivers for some databases that could be used for reactive repositories. But they are proprietary and thereby not a good basis for something that really should eventually work across all (relevant) relational databases.

一段时间以来,Spring Data团队希望 ADBA 可以填补这一空白.但是邮件列表上的讨论清楚地表明,ADBA并不是针对被动的,而只是针对异步的.再次,我们不需要反应式存储库抽象.

For some time the Spring Data team hoped that ADBA would fill that gap.But discussions on the mailing list made it clear that ADBA was not aiming for reactive but only for asynchronous.Again not what we needed for a reactive repository abstraction.

所以在2018年初,生活在十字路口或反应性和关系型的人们决定我们需要反应性数据库访问的标准.

So early in 2018 various people living at the intersection or reactive and relational decided that we need a standard for reactive database access.

R2DBC( R 有效的 R 传统的 D 数据库 A 访问)是针对这种标准的建议.希望它可以帮助说服甲骨文将ADBA转变为一种反应性方法,或者如果不这样做,它将成为标准本身.

R2DBC (Reactive Relational Database Access) is a proposal for such a standard.The hope is that it either helps convincing Oracle to move ADBA to a reactive approach or if that doesn't happen it becomes the standard itself.

并且已经有了三种实现方式,第二种选择的机会似乎是有希望的.

And with already three implementations available chances for the second option look promising.

R2DBC本身主要是SPI,即由数据库提供程序实现的API.SPI的设计方式对实施者的要求最低.但这也使R2DBC使用起来有些麻烦.这个想法是,其他库将在SPI的基础上加强并构建针对可用性而设计的库,就像JDBC一样.

R2DBC itself is mainly an SPI, i.e. an API that is to be implemented by database providers.The SPI is designed in a way that puts minimal requirements on implementers. But this also makes R2DBC somewhat cumbersome to use. The idea is that other libraries will step up and build libraries designed for usability on top of that SPI, as it happened with JDBC.

Spring Data R2DBC就是这样一个库,它提供了您所要求的:支持ReactiveCrudRepository,尽管它独立于JPA/Hibernate,并且还不支持MySQL.

Spring Data R2DBC is one such library and it offers what you asked for: Support for ReactiveCrudRepository although it is independent of JPA/Hibernate and there is no support for MySQL yet.

R2DBC和Spring Data R2DBC都尚未发布产品,并且至少要花几个月的时间.

Both R2DBC and Spring Data R2DBC didn't have a production release yet and it will take at least several months to get there.

Spring Data R2DBC刚刚发布了第一个里程碑.有关其当前功能,请参见该发布文章.

Spring Data R2DBC just released the first milestone. See the release article for its current capabilities.

R2DBC处于第6个里程碑.有关详细信息,请参见发布文章.

R2DBC is on its 6th milestone. See the release article for details.

另请参阅以下答案:

截至目前(2017年1月),这是不可能的.

Spring Data的响应部分的当前相关版本为 Spring Data Kay M1 (您可以检查是否有更新的版本在项目主页上)

The currently relevant release for the reactive part of Spring Data is Spring Data Kay M1 (You can check if there is a newer version available on the project home page)

Spring Data团队关于该发行版的博客文章,特别是其中的反应部分以(强调我的)开头:

And a blog post from the Spring Data team about that release and specifically the reactive parts in it starts with (emphasis mine):

原因是,没有标准的非阻塞方式来访问关系数据库.因此,现在仅支持那些支持此类API的应用程序.

The reason is that there is no standard non-blocking way to access a relational database. So only those that support this kind of API are supported right now.

可以使用JPA或JDBC实现ReactiveCrudRepository,并将工作委托给线程池.这样可以在外部提供一个异步API,但仍会消耗线程的资源并在独立的数据访问之间进行阻塞,因此只能实现反应式方法的一小部分好处.

One could implement a ReactiveCrudRepository using JPA or JDBC and delegate the work to a thread pool. This would provide an async API on the outside, but would still consume the resources for the Threads and block between independent data accesses, so only a small part of the benefits of the reactive approach would get realized.

这篇关于ReactiveCrudRepository在春季使用Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 21:01