1
@Query("SELECT tt, at.field, at.anotherField from TableTest tt LEFT JOIN AnotherTable at ON at.commonField = tt.commonField")
List<TestPojo> findAllPojo(List<TableTestDTO> TableTestDTOList);

How can I map this JPA query results to a Pojo without native query, like this approach ?

I'm using JPA and Hibernate. Can anyone provide other option?

Deep
  • 5,424
  • 2
  • 25
  • 33
R. Pereira
  • 155
  • 1
  • 2
  • 9

2 Answers2

0

Try using the constructor:

@Query("SELECT new TestPojo(tt, at.field, at.anotherField) from TableTest tt LEFT JOIN AnotherTable at ON at.commonField = tt.commonField")
List<TestPojo> findAllPojo(List<TableTestDTO> TableTestDTOList);

Of course such constructor must exist and even better would be to place the fully qualified name instead of bare TestPojo.

Andronicus
  • 24,333
  • 17
  • 47
  • 82
0

Well you can use the @SqlResultSetMapping annotation or you can create your own interface to reflect the query fields to it. You can find both examples here:Spring Data JPA map the native query result to Non-Entity POJO

You can call a constructor too, like @Andronicus said.

R. Karlus
  • 1,845
  • 1
  • 21
  • 42