HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
♥️
2기 최종 프로젝트 팀별 공간
/
팀 02 : 머쓱한녀석들
팀 02 : 머쓱한녀석들
/
🚀
트러블 슈팅
/
JPA fetch join 문제점

JPA fetch join 문제점

태그
jpa
속성
Done
날짜
Aug 12, 2022
작성자
 
 
  • 이미지 중복 문제점
notion image
select post0_.id as id1_3_0_, postimages1_.id as id1_4_1_, posttags2_.id as id1_5_2_, tag3_.id as id1_6_3_, couple4_.id as id1_0_4_, post0_.created_date_time as created_2_3_0_, post0_.updated_date_time as updated_3_3_0_, post0_.content as content4_3_0_, post0_.couple_id as couple_i9_3_0_, post0_.dating_date as dating_d5_3_0_, post0_.latitude as latitude6_3_0_, post0_.longitude as longitud7_3_0_, post0_.title as title8_3_0_, postimages1_.image_url as image_ur2_4_1_, postimages1_.post_id as post_id3_4_1_, postimages1_.post_id as post_id3_4_0__, postimages1_.id as id1_4_0__, posttags2_.post_id as post_id2_5_2_, posttags2_.tag_id as tag_id3_5_2_, posttags2_.post_id as post_id2_5_1__, posttags2_.id as id1_5_1__, tag3_.color as color2_6_3_, tag3_.couple_id as couple_i4_6_3_, tag3_.name as name3_6_3_, couple4_.created_date_time as created_2_0_4_, couple4_.updated_date_time as updated_3_0_4_, couple4_.start_date as start_da4_0_4_ from post post0_ inner join post_image postimages1_ on post0_.id=postimages1_.post_id inner join post_tag posttags2_ on post0_.id=posttags2_.post_id inner join tag tag3_ on posttags2_.tag_id=tag3_.id inner join couple couple4_ on post0_.couple_id=couple4_.id where post0_.id=?
notion image
 
 
 
notion image
  • 태그는 중복 제거가 되지만 이미지는 제거가 안되는 모습
 
notion image
  • 사실 태그는 set이라 중복제거가 가능한 것
 
notion image
  • 이미지는 list라 중복이 발생하게 된다.
  • 이미지도 Set으로 변경한다면?
notion image
  • 중복없이 둘 다 가져온다.
 
  • 하지만 이미지는 썸네일 이미지를 조회할 수 있어야 하기 때문에 순서가 중요하다.
notion image
 
  • 결국
notion image
select post0_.id as id1_3_0_, posttags1_.id as id1_5_1_, tag2_.id as id1_6_2_, couple3_.id as id1_0_3_, post0_.created_date_time as created_2_3_0_, post0_.updated_date_time as updated_3_3_0_, post0_.content as content4_3_0_, post0_.couple_id as couple_i9_3_0_, post0_.dating_date as dating_d5_3_0_, post0_.latitude as latitude6_3_0_, post0_.longitude as longitud7_3_0_, post0_.title as title8_3_0_, posttags1_.post_id as post_id2_5_1_, posttags1_.tag_id as tag_id3_5_1_, posttags1_.post_id as post_id2_5_0__, posttags1_.id as id1_5_0__, tag2_.color as color2_6_2_, tag2_.couple_id as couple_i4_6_2_, tag2_.name as name3_6_2_, couple3_.created_date_time as created_2_0_3_, couple3_.updated_date_time as updated_3_0_3_, couple3_.start_date as start_da4_0_3_ from post post0_ inner join post_tag posttags1_ on post0_.id=posttags1_.post_id inner join tag tag2_ on posttags1_.tag_id=tag2_.id inner join couple couple3_ on post0_.couple_id=couple3_.id where post0_.id=?
select postimages0_.post_id as post_id3_4_1_, postimages0_.id as id1_4_1_, postimages0_.id as id1_4_0_, postimages0_.image_url as image_ur2_4_0_, postimages0_.post_id as post_id3_4_0_ from post_image postimages0_ where postimages0_.post_id=?
notion image
notion image
  • List 반환이 아니라서 자동으로 중복 제거하여 반환해주는 것 같다.
    • List로 반환한다면?
    • notion image
    • DISTINCT 적용 시
      • notion image
 
  • Hibernate default_batch_fetch_size 처음엔 해당 옵션으로 해결하려 했지만 생각해보니 in절로 날리는 것이 아닌 =절로 쿼리를 수행하면 되서 필요없는 옵션이였다
 
 
dev-tips/JPA - Hibernate MultipleBagFetchException.md at master · HomoEfficio/dev-tips
다음과 같이 @OneToMany, @ManyToMany, @ElementCollection으로 1:N 또는 M:N의 관계일 때, FetchType.EAGER 로 List를 가져오면, 다음과 같이 MultipleBagFetchException 이 발생한다. 이 예외는 Hibernate 5.0.12(Spring Boot 1.5.16)과 Hibernate 5.2.17(Spring Boot 2.0.5)에서 모두 발생한다.
https://github.com/HomoEfficio/dev-tips/blob/master/JPA%20-%20Hibernate%20MultipleBagFetchException.md
(Troubleshooting) Hibernate MultipleBagFetchException 정복하기
Spring Data JPA를 이용하다보면 종종 org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags이란 메세지를 보게 된다.우선 어떤 상황에 나타나는지 한 번 살펴보자. 엄마가 있고, 아들/딸들이 있는데 아들/딸들을 EAGER로 fetch해 올 때 발생한다. (즉, OneToMany, ManyToMany인 Bag 두 개 이상을 EAGER로 fetch할 때 발생한다.) EAGER로 땡겨오면 N+1 쿼리 문제가 존재하기 때문에 fetchType을 전부 LAZY로 바꾼 후 한 방 쿼리로 불러와도 문제는 재발한다.
https://perfectacle.github.io/2019/05/01/hibernate-multiple-bag-fetch-exception/
MultipleBagFetchException 발생시 해결 방법
JPA의 N+1 문제에 대한 해결책으로 Fetch Join을 사용하다보면 자주 만나는 문제가 있습니다. 바로 MultipleBagFetchException 입니다. 이 문제는 2개 이상의 OneToMany 자식 테이블에 Fetch Join을 선언했을때 발생합니다. OneToOne, ManyToOne과 같이 단일 관계의 자식 테이블 에는 Fetch Join을 써도 됩니다 이 문제에 대한 해결책으로 보통 2가지를 언급하는데요.
https://jojoldu.tistory.com/457?fbclid=IwAR132BRMYHrL4D5Pu25YUglIcEN1FGTE2tacFcsVOPAT0MAzwoMX6Flzbe0
[JPA] Fetch Join 할 때 MultipleBagFetchException 해결하는 법
이번 글에서는 JPA 를 사용한지 얼마 안된 초보자 입장에서 겪은 어려움을 공유하고 어떻게 해결해나갔는지 공유하며 생각을 정리해보려 합니다. 위의 화면에 대한 API를 만들어야 하는 상황입니다. 위의 뷰에서는 내가 팔로우 하고 있는 사람이 작성한 게시글 이 나타나야 합니다. View를 보면서 DB Table 을 간단하게 같이 보겠습니다.
[JPA] Fetch Join 할 때 MultipleBagFetchException 해결하는 법
https://devlog-wjdrbs96.tistory.com/421
JPA 에서 별칭을 쓰지 않는 이유 (하지만 쓴 이유)
Intro JPA의 사용시 별칭을 쓰면 안되는 이유가 무엇인지 알아본다. 프로젝트애서 fetch join 시 별칭 사용에 대해서 고민해본다. fetch join 별칭은 왜 안될까 ? fetch join에서 별칭이 안되는 이유는 데이터의 일관성이 깨지기 때문이다. 예를 들어서 다음과 같은 코드는 fetch join 대상에 조건문이 들어가서 일관성이 깨진 경우이다. TeamA에 대한 member collection 은 본래 3개이다.
https://yjksw.github.io/jpa-fetch-join-nickname/
JPA의 사실과 오해 (사실은 JPQL과 Fetch Join의 오해)
20.05.25 JPA 스터디 하며 깨닫게 된 오해 JPA를 꽤나 오래 써왔는데(그래봤자 1년이긴 하지만), 이번에 스터디를 하며 크게 잘못 알고 있던 개념 몇가지를 정정하고, 스스로 부끄럽기도 하고 나름의 컬쳐쇼크를 받았다. ( 주의 - JPQL 기준, SQL X ) 사실 왜 (당시 스터디하던) 우리 모두가 N+1 문제를 연관 엔티티의 컬렉션 여부와 관련지어 생각해왔던 건지 모르겠다.
https://os94.tistory.com/201
A Guide to MultipleBagFetchException in Hibernate | Baeldung
In this tutorial, we'll talk about the MultipleBagFetchException. We'll begin with the necessary terms to understand, and then we'll explore some workarounds until we reach the ideal solution. We'll create a simple music app's domain to demonstrate each of the solutions. A Bag, similar to a List, is a collection that can contain duplicate elements.
A Guide to MultipleBagFetchException in Hibernate | Baeldung
https://www.baeldung.com/java-hibernate-multiplebagfetchexception
MultipleBagFetchException 발생시 해결 방법
JPA의 N+1 문제에 대한 해결책으로 Fetch Join을 사용하다보면 자주 만나는 문제가 있습니다. 바로 MultipleBagFetchException 입니다. 이 문제는 2개 이상의 OneToMany 자식 테이블에 Fetch Join을 선언했을때 발생합니다. OneToOne, ManyToOne과 같이 단일 관계의 자식 테이블 에는 Fetch Join을 써도 됩니다 이 문제에 대한 해결책으로 보통 2가지를 언급하는데요.
https://jojoldu.tistory.com/457