我需要为自定义查询编写一个Spring Data存储库方法,并希望使用基于类的投影。
参考此文档: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工作正常
- findDistinctQuery和findDistinctNativeQuery会抛出异常
找不到转换器,无法将类型[org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap]转换为类型[com.x.PersonDTO]
是否有任何选项可使其与类(而非接口)配合使用?