How to do native query with paging?

Asked

Viewed 103 times

1

I’m managing to make a simple select, but I don’t know the structure of how to make a select using Join. Follow the code below:

That code works:

   @Query(value = "SELECT * FROM tabela1  /*#pageable*/",
   countQuery = "SELECT count(*) FROM tabela1",
   nativeQuery = true)
   Page<ClasseModeloTabela> testePaginacaoQuery(Pageable pageable); 

This code does not work the syntax is wrong?

 @Query(value = "select i.* " + "from tabela1 r 
 /*#pageable*/"
 + "join tabela2 ri on ri.chavetabela2 = 
 r.chavetabela1 "
 + "join tabela3 i on i.chavetabela3 = 
 ri.chavetabela2"
 + "where r.NR_campo1 = ?1 or i.campo2 =?2 ",
 countQuery = "SELECT count(*) FROM tabela1",
 nativeQuery = true)
 public Page<ClasseModeloTabela> testePaginacaoQueryJoin(Long campo1, 
 Long campo2, Pageable pageable);

1 answer

0


I already found the answer, for those who have interested the syntax is the following:

@Query(value = "select i.* " + "from tabela1 r  "
+ "join tabela2 ri on ri.chavetabela2 = r.chavetabela1  "
+ "join tabela3 i on i.chavetabela3 = ri.chavetabela2 "
+ "where r.campo1 = ?1 or i.campo2 =?2 /*#pageable*/ ",
 countQuery = "SELECT count(*) "
+"from tabela1 r  "
+ "join tabela2 ri on ri.chavetabela2 = r.chavetabela1  "
+ "join tabela3 i on i.chavetabela3 = ri.chavetabela2 "
+ "where r.campo1 = ?1 or i.campo2 =?2  ",
   nativeQuery = true)
      public Page<ClasseModeloTabela> testePaginacaoQueryJoin(Long campo1, Long campo2, Pageable pageable);

Browser other questions tagged

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