Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[정보] fetch join 시 record를 불러오지 못하는 문제 해결 #56

Open
park0503 opened this issue May 24, 2022 · 1 comment
Open
Assignees

Comments

@park0503
Copy link
Member

다음과 같이 travel에 속한 user들의 정보와 travel의 정보를 모두 불러올 수 있도록 fetch join을 이용해 jpql 쿼리를 만들어 사용할 수 있다.

@Query("select distinct t from Travel t join fetch t.userTravels ut join fetch ut.user where t.id = :travelId")
Optional<Travel> findTravelById(Long travelId);

그러나 이럴 경우 테스트에서 RecordNotFoundException이 간혹 발생함을 확인할 수 있었다. (해당 exception이 항상 발생하지 않는 점이 원인 파악의 힌트가 되었다.) 이는 fetch join이 쿼리 레벨에서 inner join의 형태로 작동해 travel과 연관된 userTravel이 없을 경우 아무런 레코드를 반환해주지 않는 것이 원인이었다.
따라서 다음과 같이 left outer join을 이용한 fetch join을 이용함으로써 해결할 수 있었다.

@Query("select distinct t from Travel t left join fetch t.userTravels ut left join fetch ut.user where t.id = :travelId")
Optional<Travel> findTravelById(Long travelId);
@Sophoca
Copy link
Member

Sophoca commented May 25, 2022

미처 신경쓰지 못한 부분이네요. 좋은 내용 공유 감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants