황당한 오류로 한참을 해맸다.
Jpa를 쓰다보면 지원되는 repository 보다 커스텀 매서드를 많이 쓰게 되기 마련인데
커스텀 매서드를 작성하고 빌드하면 커스텀 매서드를 entity 내에서 찾을수 없다는 메세지만 뱉어냈었다.
단순하게 custom 매서드 오타인줄 알았고 그전에도 쭉 비슷하게 패키지를 구성해왔기 때문에
( 다른 토이 프로젝트도 비슷하게 구현.)
당연히 될줄 알았는데 문제는 엉뚱한곳에 있었다.
첫번째는 네이밍 규칙을 지키지 않았고
두번째는 패키지 구성이 올바르지 않다는것을 확인했다.
Repository 네이밍을 생성할때 주의해서 작성을 해야한다.
예를 들어
Entity 네임이 Book 이라고 가정한다면 구성과 네이밍은 아래와 같이 해야한다.
Book
BookRepository
BookRepositoryCustom
BookRepositoryCustomImpl
특히 BookRepository / BookRepositoryCustom /BookRepositoryCustomImpl
인터페이스(interface) 와 구현체(implement)는 같은 패키지에 두어야 한다.
본인은 아래와 같이 두었다.
repository
- impl
-xxxRepositoryCustomImpl
- xxxRepository
- xxxRepositoryCustom
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom {
...
}
public interface BookRepositoryCustom {
Book findByBookIdx(Long bookIdx);
QueryResults<Book> getSearchBookList(String searchKeyword);
}
@Slf4j
public class BookRepositoryCustomImpl implements BookRepositoryCustom {
...
}
## 참고
No property found for type... custom Spring Data repository
I'm trying to implement a custom Spring repository. I have the interface: public interface FilterRepositoryCustom { List<User> filterBy(String role); } the implementation: public class
stackoverflow.com
Spring Data JPA - Reference Documentation
Example 109. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del
docs.spring.io