jpaRepository + JPQL + JOIN

Asked

Viewed 692 times

1

I have the query below running in my code but using the real names of the table and fields, in this same structure below works, but how do I do to do the same but using the language JPQL, I am using generic table names and fields only to facilitate understanding.

@Query(value = "select rc.* from tabela1 i  join tabela2 rc on rc.campoA= i.campoA")                             
public Page<Tabela2> trazerResultado(Pageable pageable);

1 answer

0


Understanding that the entities Table 1 and Table 2 do not have a relationship with each other, and you just want to make a JOIN between them, we can do this via WHERE:

@Query(value = "SELECT tab2 FROM Tabela1 tab1, Tabela2 tab2 
    WHERE tab1.campoA = tab2.campoA")

In newer versions hibernate (from 5.1) you can use ON also, enabling consultations between entities without relationship:

@Query(value = "select rc from Tabela1 tab1 
   JOIN Tabela2 rc ON rc.campoA = tab1.campoA")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.