MongoDB 스프링부트 Read 메서드 에러 - Unsatisfied dependency expressed through constructor parameter ~

리진아·2024년 12월 27일
0

PolishPix

목록 보기
6/7

몽고db와 스프링부트 연동 후 조회하는 메서드를 생성 중


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postController' defined in file [/Users/jina-lee/git/PolishPix_Server/ROOT/bin/main/com/main/sub/PostController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'postRepository' defined in com.main.sub.Repository.PostRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Could not create query for public abstract java.util.List com.main.sub.Repository.PostRepository.findByUser_User_id(java.lang.String); Reason: No property 'user' found for type 'User'; Traversed path: Post.user

이런 에러 발생.


{
  "_id": {
    "$oid": "6769045869c1ee4f6f3b6271"
  },
  "post_content": "첫번째 포스팅.",
  "insert_date": "2024-12-23",
  "user": {
    "user_id": "1",
    "user_name": "이진아",
    "user_profile": "https://i.ibb.co/B4Z4Ptp/IMG-1184.jpg"
  },
  "images": [
    {
      "image_id": "1",
      "image_url": "https://i.ibb.co/pj7smsx/Kakao-Talk-Photo-2024-12-23-15-29-36-001.jpg"
    },
    {
      "image_id": "2",
      "image_url": "https://i.ibb.co/Ptxcmn2/Kakao-Talk-Photo-2024-12-23-15-29-37-002.jpg"
    }
  ]
}

위와 같은 데이터 구조에서 user_id만 가지고 post 컬렉션 데이터를 조회하고 싶었다.

public interface PostRepository extends MongoRepository<Post, String> {
    // User 객체 내의 user_id를 기준으로 게시글을 찾는 메서드
	List<Post> findByUser_User_id(String user_id);
	
}

User.user_id를 못찾는 건가 싶어서 이리저리 findByUser_User_id 명만 바꿔보고 post 메서드에서 userId도 바꿨는데

@Query("{'user.user_id': ?0}")
    List<Post> findByUserId(String user_id);

이렇게 @Query 어노테이션 형식으로 바꾸니까 되더라

결론 : 쿼리 언어를 직접 사용하니까 해결

profile
이것저것 개발 블로그

0개의 댓글

Powered by GraphCDN, the GraphQL CDN