• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

기존 project에 Querydsl 적용 문제입니다

22.11.16 21:22 작성 조회수 1.03k

0

기존에 작성하던 코드에 querydsl을 붙였는데 문제가 뜨네요..
빈 프로젝트 test코드에선 잘 작동하였는데

UnsatisfiedDependencyException이 계속 뜨네요..

jparepository에 extend 하는 순간에 에러가 뜹니다.

커스텀 파일 (PostRepositoryCustom,PostRepositoryImpl)

두 클래스만 연결하고 run을 돌렸을땐 에러가 안뜹니다.

그런데, jpa repository을 상속받은 PostRepository에 extend로 PostRepositoryCustom을 연결해주는 순간 저렇게 에러가 뜨네요...

구글링해봐도 이유를 모르겠습니다 제발 도와주세요

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'likeController' defined in file [E:\dev\spring\week6\final\build\classes\java\main\com\example\soldapple\like\controller\LikeController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'likeService' defined in file [E:\dev\spring\week6\final\build\classes\java\main\com\example\soldapple\like\service\LikeService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'postRepository' defined in com.example.soldapple.post.repository.PostRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.soldapple.quarydsl.PostRepositoryCustom.findAllMyTest(); Reason: Failed to create query for method public abstract java.util.List com.example.soldapple.quarydsl.PostRepositoryCustom.findAllMyTest()! No property 'findAllMyTest' found for type 'Post'; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.soldapple.quarydsl.PostRepositoryCustom.findAllMyTest()! No property 'findAllMyTest' found for type 'Post'

답변 1

답변을 작성해보세요.

0

y2gcoder님의 프로필

y2gcoder

2022.11.16

안녕하세요. S-J L님, 공식 서포터즈 y2gcoder입니다.

해당 에러 로그만 봤을 때는 에러 원인 분석을 위해 PostRepositoryImpl 코드와 Post 의 코드가 필요합니다.

Failed to create query for method public abstract java.util.List com.example.soldapple.quarydsl.PostRepositoryCustom.findAllMyTest()! No property 'findAllMyTest' found for type 'Post'

 

여기서 Post 엔티티에 findAllMyTest 프로퍼티를 찾을 수 없다고 나오는데, PostRepositoryImpl에서 post 객체를 어떻게 사용하고 있는지 알아야 도와드릴 수 있을 것 같습니다.


감사합니다.

S-J L님의 프로필

S-J L

질문자

2022.11.16

image

김영한님 강의 예제와 유사하게 작성하였습니다.

그런데, imple 위에 @Component를 넣으니 run이 일단 무리 없이 되는데 왜 이런건지 설명 부탁드려도 될까요 ??

y2gcoder님의 프로필

y2gcoder

2022.11.17

말씀해주신 것만 봤을 때는 사용자 정의 리포지토리 처럼 인식한 게 아니라 따로 분리해서 만든 Querydsl 리포지토리 처럼 등록된 것 같습니다. 저기에 @Repository를 붙여도 똑같이 작동할 것 같습니다.

보여주신 예외와 일부 코드만 가지고 판단해봤을 때는

https://velog.io/@byeongju/JPA-Failed-to-create-query-for-method-public-abstract
와 같이 JpaRepository에서 자동으로 쿼리를 만드는 기능과 직접 만드신 Querydsl 부분이 충돌난 것 같습니다. 

그래서 따로 빈으로 등록해서 사용할 때는 정상적으로 작동한 것 같습니다.

위에 에러로그대로라면 findAllMyTest 부분이 의심스러운데 메서드명을 혹시 아예 다른 것으로 바꿔도 예외가 발생할까요?

S-J L님의 프로필

S-J L

질문자

2022.11.17

방금 테스트해보니 메서드명을 바꾸고 @Component 어노테이션을 떼니 오류가 나네요
@ Component를 붙이면 다시 되구요.
그런데 실질적으로 저 인터페이스와 구현체들을 상속받아서 service단에서
postRepository.findAllMyTest(~) 로 사용을 해도 작동에는 무리가 없네요

혹시 @Component를 계속 다는 형태로 코드를 작성하는게 추후에 비효율적인 문제를 야기시킬까요?
그렇지만 않으면 일단 이런 방식으로 해볼려고 합니다 ㅠㅠ

y2gcoder님의 프로필

y2gcoder

2022.11.17

혹시 @Component 대신에 @Repository를 달아도 오류가 나시나요?

오류가 발생하지 않는다면 @Repository를 붙이고 하시길 추천드립니다. 제가 전체코드를 볼 수 없어 확답을 드릴 수 없지만, 현재 방식이 Querydsl용 Repository를 따로 만들어 사용하는 방식과 유사하게 작동하고 있는 것이라 보입니다. 실제로 JPARepository 인터페이스에 사용자 정의 리포지토리를 만들지 않고 따로 Querydsl 용 Repository를 만들어 사용하시는 분들도 있어서 상관 없을 것 같습니다.

 

@Repository로 붙여달라고 말씀드리는 이유는 좀 더 명확하게 역할을 표현하기 위해 + 데이터베이스 플랫폼 별로 오는 예외를 추상화해주기 때문입니다.