Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: support for Hibernate reactive #2211

Closed
yaakov-berkovitch opened this issue May 3, 2021 · 8 comments
Closed

Enhancement: support for Hibernate reactive #2211

yaakov-berkovitch opened this issue May 3, 2021 · 8 comments
Assignees
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@yaakov-berkovitch
Copy link

yaakov-berkovitch commented May 3, 2021

Is there any plan to support Hibernate Reactive project working with the Vert.x Reactive DB driver

We started a project using this stack and Spring boot, but it will be very valuable to be able to use also spring-data.

Thanks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 3, 2021
@mp911de mp911de self-assigned this May 3, 2021
@mp911de
Copy link
Member

mp911de commented May 3, 2021

Hibernate Reactive isn't using the JPA API, rather they introduced new API that is specific to Hibernate. If you're looking for reactive SQL support, take a look at Spring Data R2DBC that integrates with reactive drivers following the R2DBC standard.

@mp911de mp911de closed this as completed May 3, 2021
@mp911de mp911de added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels May 3, 2021
@yaakov-berkovitch
Copy link
Author

Thanks @mp911de for your answer. I didn't know they defined a new API. I found it very promising because we already have a big knowledge around regular Hibernate, but we are moving to reactive programing. The problem is that RD2BC has some missing features like lazy loading, support for association like one-to-many, caching and more.

@brianpreuss
Copy link

Support for Hibernate Reactive would have been very nice

@UkonnRa
Copy link

UkonnRa commented Dec 21, 2021

+1, supporting Hibernate Reactive would be nice, since R2DBC does not support QueryDSL yet.

@ah1508
Copy link

ah1508 commented Dec 27, 2021

+1 , the big advantage is the support of the JPA mapping. The reactive session API is not JPA compliant but it does not matter since spring data is an abstraction on top of it. However it would be a new spring data module.

Hibernate reactive uses Mutiny but a converter helps expose Mono and Flux instead of Uni and Multi. Here is a basic implementation of ReactiveSortingRepository with Hibernate reactive : spring-data-hibernate-reactive.zip

  • with SessionFactory auto configuration and second level cache filling on startup
  • without ReactiveTransactionManager.

It also uses vert.x database driver. It results in two event loops if used with spring-webflux but not with vertx-spring-boot-starter-http.

Query by example and derived queries handling could be taken from spring-data-jpa (except operation that involves a count query).

The tests use postgresql (docker-compose.yml included in the project). Only the delete by entity and entities fails since find and remove are not called with the same Session because the test method execution is not surrounded by a transaction.

Lazy loading is obviously not supported but join fetch and eager loading (with second level cache) help to handle most of the relations loading use cases.

For lazy relations, an interesting feature could be :

Mono<T> findById(ID id, String... relationsToFetch);

Usage :

Mono<Blog> mono = repository.findById(1, "posts");

Since Mutiny.fetch allows lazy loading it is maybe possible to declare a method like this, which brings type safety.

Mono<T> findById(ID id, Function<T, ?>... relationsToFetch);

Usage :

Mono<Blog> mono = repository.findById(1,  x -> x.getPosts());

However loading 1-n and n-n relations in a single query results in n lines returned from the database (and Cartesian product if several 1-n or n-n relations are fetched).

@hantsy
Copy link

hantsy commented Feb 21, 2022

@ah1508 I have added Mutiny support to Spring framework and Spring Data Commons, so you can use Mutiny in Spring directly, no need conversion explicitly, but Spring Data Commons did not accept the Mutiny variants of the Repository interface.

Hibernate Reactive reused the annotations of JPA, for most of Java developers who are familiar with the Hibernate Session or JPA EntityManager, in contrast to using(or learn) Spring Data R2dbc, the cost is lower when migrating to Hibernate Reactive.

Compared to the existing Hibernate support in Spring, when adding Hibernate Reactive the biggest challenge is providing a reactive SessionFactoryBean instead, and also should support Reactive transaction. Then implementing the Spring Data Repository support.

@hantsy
Copy link

hantsy commented Feb 21, 2022

My example project for adding Hibernate Reactive to Spring manually, https://itnext.io/integrating-hibernate-reactive-with-spring-5427440607fe.

@Sam-Kruglov
Copy link

Hibernate Reactive is not against introducing R2DBC into their codebase but are not willing to put in the work themselves to do that. However, they added an extension point that allows users to experiment with integrating alternative reactive database drivers.

see hibernate/hibernate-reactive#76 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

8 participants