Search code examples
quarkus-panachequarkus-rest-clientquarkus-reactive

Is it possible to join two entities with panache?


I want to get two values from two different sql tables.

In sql we would do "Select a.value, b.value from A a inner join B b on a.id = b.id where ..."

Is it possible to do it with panache? Like join two entities or something like that?

Can't really find much about joins with quarkus panache.


Solution

  • Panache doesn't have any specific way to deal with joins.

    You can still load specific fields using HQL and projections:

    List<EntitiesView> results = EntityA
        .find("Select a.value, b.value from A a inner join B b on a.id = b.id where ...")
        .project(EntitiesView.class)
        .list(); 
    

    Where EntitiesView is:

    @RegisterForReflection 
    class EntitiesView {
       public final Object valueA;
       public final Object valueB;
    
       public EntitiesView(Object valueA, Object valueB) {
           this.valueA = valueA;
           this.valueB = valueB;
       }
    }
    

    You can find more details about projections on the Panache guide.