Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 309
SoT Avatar asked Jan 27 '26 00:01

SoT


1 Answers

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(...);
}
like image 181
Ferten Avatar answered Jan 28 '26 17:01

Ferten



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!