TIL 23.02.15

쓰옹·2023년 2월 15일
0

개발자를 향해~~TIL✍

목록 보기
87/87

participant와 user는 ManyToOne으로 연관관계가 맺어져있다.
처음에
participantRepository.findByUserIdOrderByCreatedAtDesc(Long userId)
요렇게 했을 때 오류가 발생했다.

Reason: Failed to create query for method public abstract java.util.List com.saemoim.repository.ParticipantRepository.findAllByUserIdOrderByCreatedAtDesc(java.lang.Long); Unable to locate Attribute with the given name [userId] on this ManagedType

찾아보니까 보통 오타가 있을 때 발생하는 오류던데 아무리 찾아도 못찾겠더라.. 그래서 객체 자체를 참조했다.
`participantRepository.findByUserOrderByCreatedAtDesc(User user)
근데 저번 프로젝트 땐 userId로 하는게 된 것 같은데 왜 안될까를 생각하다가 _를 넣으면 left join으로 된다는 얘기를 듣고 한 번 해봤다.
participantRepository.findByUser_IdOrderByCreatedAtDesc(Long userId)
이렇게 하고 쿼리를 확인하기 위해 테스트를 했는데

// test

 /* <criteria> */ select
        p1_0.id,
        p1_0.created_at,
        p1_0.group_id,
        p1_0.modified_at,
        p1_0.user_id 
    from
        participant p1_0 
    where
        p1_0.user_id is null 
    order by
        p1_0.created_at desc

left join이 아닌 그냥 원래 원하던 쿼리가 날라갔다. 객체로 넣을 때랑 같은 쿼리다. 아무리 찾아봐도 차이가 안나오고 이전 프로젝트와 차이를 보니 스프링부트 3.0.2를 사용했다. 그래서 달라진게 있나 찾아봤는데 Spring Data JPA - Reference Documentation를 보면 잘 알아들은건지 아닌지 확실하진 않지만 it is possible for the algorithm to select the wrong property. 알고리즘이 실수할수도 있으니? _를 넣어라 뭐 이런 것 같다. 근데 이것도 확실하진 않다. 그리고 다른 팀원분이 작업한 부분에서 commentRepository.findAllByPostId(Long postId) 여기서는 또 잘 작동을 한다. 쿼리도 left join이 아니라 위에처럼 날라간다. _를 넣지 않아도!! 뭔 차인지를 모르겠다.. 일단 _넣어서 쿼리 잘 날라가는걸 확인했으니 객체를 참조하고 있던 부분을 수정해서 다른 repository 의존성을 줄였다.

profile
기록하자기록해!

0개의 댓글