Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

java - 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"

question from:https://stackoverflow.com/questions/65950985/r2dbc-reactivecrudrepository-how-to-write-jpql-hql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(...);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...