Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use hibernate with spring web flux

I am new to reactive programming I have started using webflux, previously I work on spring boot there I have used hibernate as a ORM framework. My doubt is what is replace of hibernate in reactive stack , which framework I have to use to connect & implement database logic. I am using mongoDB.

Thanks in advance.

like image 692
Pramod Kumar Avatar asked Oct 23 '25 11:10

Pramod Kumar


2 Answers

you have to use either R2DBC or Hibernate reactive. if you migrating an old service from spring boot to webflux, I recommended you to use Hibernate reactive.

when you use R2DBC, you can't use hibernate mappings and annotations.

according to hibernate reactive documentation:

When using a stateless session, you should be aware of the following additional limitations:

  • persistence operations never cascade to associated instances,
  • changes to @ManyToMany associations and @ElementCollections cannot be made persistent, and
  • operations performed via a stateless session bypass callbacks.
like image 194
Issa Khodadadi Avatar answered Oct 26 '25 04:10

Issa Khodadadi


Hibernate is based on JDBC. JDBC is blocking. Blocking APIs don't work well in reactive stacks. Also, Hibernate under the hood uses ThreadLocals which makes it even worse candidate for reactive applications.

For Webflux, as the alternative to Hibernate you should look into Spring Data R2DBC which does the basic database result to Java object mapping, but keep in mind that it's not a full fledge ORM like Hibernate.

You may also want to give it a try to Hibernate Reactive. With this you can use the power of Hibernate mappings in a reactive non blocking way. One thing that will not work though (at least not yet) is declarative transaction management with @Transactional.

like image 20
Maciej Walkowiak Avatar answered Oct 26 '25 03:10

Maciej Walkowiak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!