티스토리 뷰

안녕하세요.
Eetch Join에 대해 포스팅해보겠습니다.

WithOut FetchJoin

  @PersistenceUnit
  EntityManagerFactory emf;

  @Test
  public void fetchJoinNo() throws Exception {
      em.flush(); // fetch test시 해주는게 좋음
      em.clear();

      Member member1 = queryFactory
              .selectFrom(member)
              .where(member.username.eq("member1"))
              .fetchOne();

      // 1. 회원만 조회 (팀X)
      boolean loaded = emf.getPersistenceUnitUtil().isLoaded(member1.getTeam());
      assertThat(loaded).as("패치 조인 미적용").isFalse();
  }

with FetchJoin

@Test
public void fetchJoinUse() throws Exception {
    em.flush();
    em.clear();

    Member member1 = queryFactory
            .selectFrom(member)
            .join(member.team, team).fetchJoin()
            .where(member.username.eq("member1"))
            .fetchOne();

    // 1. 회원 + 팀 fetch join
    boolean loaded = emf.getPersistenceUnitUtil().isLoaded(member1.getTeam());
    assertThat(loaded).as("패치 조인 적용").isTrue();
}

이전에 영속성컨텍스트에 남아있는 것들을 초기화하기위해 flush, clear

native query

select
    member0_.member_id as member_i1_1_0_,
    team1_.team_id as team_id1_2_1_,
    member0_.age as age2_1_0_,
    member0_.team_id as team_id4_1_0_,
    member0_.username as username3_1_0_,
    team1_.name as name2_2_1_ 
from
    member member0_ 
inner join
    team team1_ 
        on member0_.team_id=team1_.team_id 
where
    member0_.username=?

Fetch Join을 잘 활용하면 성능을 향상시킬 수 있습니다.

감사합니다.

참고

www.inflearn.com/course/Querydsl-%EC%8B%A4%EC%A0%84#

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함