I need to have a spring data repository method for a custom query and would like to use class based projection.
Looking at this https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
@Entity
public class Person {
@Id
private Long id;
private String firstName;
private String lastName;
private int age;
}
@Value // lombok annotation to create constructor, equals and hash-code
public class PersonDTO {
private String firstName;
private String lastName;
}
public interface PersonRepository extends Repository<Person, Long> {
List<PersonProjection> findDistinct();
@Query("select distinct firstName, lastName from Person")
List<PersonProjection> findDistinctQuery();
@Query(value = "select distinct first_name, last_name from person", nativeQuery = true)
List<PersonProjection> findDistinctNativeQuery();
}
- findDistinct works well
- findDistinctQuery and findDistinctNativeQuery throw
No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.x.PersonDTO]
Is there any option to make it work with classes (not interfaces)?