오늘도 어김없이 나타나는 에러,,,
에러는 정말이지 매일매일 만나는 것 같다.
오늘 만난 에러는 다음과 같다.
org.springframework.beans.factory.UnsatisfiedDependencyException
...
Reason: Failed to create query for method public abstract
java.util.Optional jpabook.dashdine.repository.Restaurant.RestaurantRepository.findByIdAndUserIdAndDeletedFalse(java.lang.Long,java.lang.Long);
Unable to locate Attribute with the given name [deleted] on this ManagedType
뭐 대충 보니까 RestaurantRepository에서 정의한 메서드 findByIdAndUserIdAndDeletedFalse의 쿼리를 잘못 작성한 거 같다는 내용이었다.
Optional<Restaurant> findByIdAndUserIdAndDeletedFalse(Long id, Long userId);
여기서 deleted 라는 필드값을 찾을 수 없다는 거 같다.
나의 의도는 각 id 값을 파라미터로 받고, isDeleted 가 false 인 것만
즉, 논리적으로 삭제가 되지 않은 객체만 조회하고 싶었다.
private boolean isDeleted;
기존에는 위와 같이 필드가 있었는데,,, JPA 메서드 생성시에는 is 라는 키워드를 감지를 못하는 거 같다 ?!
기존 엔티티에 존재하던 필드의 이름을 변경해주었다
isDeleted -> deleted
흠,,, 그데 논리 필드에서는 is 를 명시해주고 싶은데,,,
기존 필드명을 유지하기 위해서는 JPA 메서드 말고 JPQL 이나 QueryDSL 을 쓰든가 해야겠다.
물론 entity 자체에 @Where 어노테이션을 통해서 조회 간 논리삭제 된 테이블은 조회하지 않도록 할 수는 있지만, 이는 유연성이 떨어진다고 판단해 보류하였다.
JPA 메서드 생성 간 is 키워드는 인식을 못하는 거 같다..!
혹시 몰라
Optional<Restaurant> findByIdAndUserIdAndIsDeletedFalse(Long id, Long userId);
이와 같이 작성하니 의도한 대로 결과가 출력되었다.
메서드 작성할 때 자동완성으로 IsDeleted 가 나오질 않아서 안되는 줄 알았으며, 분명 위처럼 작성했을 때
Cannot resolve property 'isDeleted'
이런 문구가 명시되길래 당연히 안되는 줄 알았다,,,
이제라도 알게되어서 다행인 거 같다.