Search code examples
javahibernatespring-webfluxspring-data-r2dbc

r2dbc ReactiveCrudRepository how to write JPQL/HQL


I'm new to Spring WebFlux reactive. I use R2DBC postgresql. I have a repository like that:

public interface BookRepository extends ReactiveCrudRepository<Book, Long> {
}

Now I want to add custom method to query by many complicated conditions:

public interface CustomBookRepository extends BookRepository {
    Flux<Book> findByVeryComplicatedCondition(MyCriteriaDto criteria);
}

My implementation:

public class CustomBookRepositoryImpl extends CustomBookRepository {

    //How to get it?
    EntityManager em;

    @Override
    public Flux<Book> findByVeryComplicatedCondition(MyCriteriaDto criteria) {
        Query query = em.createQuery("SELECT b from Book b WHERE (VERY COMPLICATED CONDITIONS)");
        //What next?
    }
}

My questions are in the code above:

  1. How to obtain an EntityManager?
  2. How to get Flux from HQL query I built?

When I ask these questions, I mean "How to do this with Spring reactive/r2dbc way", not "How to do this normal way with JDBC"


Solution

  • If you want to use Hibernate/JPA JPQL like query in your project, consider Hibernate Reactive.

    I have written an article to describe how to use Hibernate Reactive with Spring (BTW, I have contributed a patch to register SmallRye in Spring framework, so at the end of this article, you can skip to register MutinyAdapter in ReactiveAdapterRegistry ).

    1. Spring Data has no plan to integrate Hibernate Reactive, so there is no Repository support.
    2. Hibernate Reactive depends on Vertx reactive drivers, it does not support R2dbc.
    3. HibernateReactive supports another ReactiveStreams implemetnation from Redhat - Smallrye Munity.