Spring Data JPA native query result entity

When you create navite query which contains results from multiple tables (after joins) you don’t have to create new entities.

Better way to solve this problem is to projection with interface or class DTO. For example, if you want to combine results from Person and Address Entities, simply create interface:

public interface PersonView {
    String getFirstName();
    String getLastName();
    String getStreet();
}

You can see combined fileds from Person (firstName, lastName) and Address (street). You have to use it as query response, like this:

@Query(...)
List<PersonView> getPersonWithStreet(String state);

You can read more about it here:

https://www.baeldung.com/spring-data-jpa-projections https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top