3

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"

SoT
  • 898
  • 1
  • 15
  • 36
  • did you read the documentation for spring R2DBC before you asked here? – Toerktumlare Jan 29 '21 at 08:28
  • As I mentioned, I'm new to Spring WebFlux. I've already read some documents but they are about R2DBC MongoDB. I cannot find any resource about custom HQL – SoT Jan 29 '21 at 08:31
  • well if you actually looked up what HQL is, it stands for Hibernate Query Language, and Hibernate is not supported in R2DBC, which is easy to google, if you google R2DBC and Hibernate. Also if you google `R2DBC documentation` you will be taken to the official documentation and there you can read all about what it supports and what you can and cannot do. https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users – Toerktumlare Jan 29 '21 at 08:34
  • just because you are new, doesn't mean you arn't allowed to read documentation, on the contrary, when you are new you should be reading the documentation first, before asking. – Toerktumlare Jan 29 '21 at 08:37
  • If I cannot use HQL, could you suggest me another mechanism which support in querying with complicated conditions like that? – SoT Jan 29 '21 at 08:38
  • how about just plain regular SQL? as said, read the docs, what is "complicated" is relative. You have not stated your exact demands, neither have you actually tried anything yet. – Toerktumlare Jan 29 '21 at 08:39

2 Answers2

11

Spring Data R2DBC is not backed by a fully-fledged ORM framework like Hibernate. Therefore there are no such things as EntityManager and no possibility to write JQL/HQL queries.

However, one can still use native queries to define more complex methods, e.g.

interface MyRepository extends ReactiveCrudRepository<...> {

    @Query("SELECT b from Book b WHERE (VERY COMPLICATED CONDITIONS)")
    Flux<...> find(...);
}
Ferten
  • 141
  • 4
  • Hi Ferten, my case is not just about \@Query. I need to write custom query statement based on user input, so \@Query have no luck here :( – SoT Feb 04 '21 at 15:36
1

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.
Hantsy
  • 8,006
  • 7
  • 64
  • 109