자바/스프링
Spring Data JPA DTO 사용 시 매핑 에러가 발생할 때(org.springframework.data.mapping.PropertyReferenceException: No property ...)
땅부자몽구스
2021. 10. 8. 22:33
Entity가 아닌 DTO를 리턴하는 커스텀 Repository 메서드를 작성할 때, 메서드 이름에 "By 키워드가 없는 경우" 아래와 같은 매핑 에러가 발생한다.
org.springframework.data.mapping.PropertyReferenceException: No property ... found for type Entity 클래스 이름!
즉, 조건문 없이 DTO List를 리턴하는 커스텀 findAll 메서드를 작성할 때도 메서드 이름 마지막에 By 키워드를 넣어야 한다.
public interface UserRepository extends JpaRepository<User, Long> {
List<UserNameDto> findUserNameListBy();
}
By 키워드는 Entity의 일부 데이터를 조회하는 등의 파생쿼리를 실행하겠다는 의미이다.
출처:
How to use projection interfaces with pagination in Spring Data JPA?
I'm trying to get a page of a partial entity (NetworkSimple) using the new feature of spring data, projections I've checked the documentation and if I request only: Collection
stackoverflow.com
Ultimate Guide: Derived Queries with Spring Data JPA
Spring Data JPA generates simple queries based on the name of a method in your repository. Your method names just have to follow a simple pattern, which I show you in this guide.
thorben-janssen.com