๊ด€๋ฆฌ ๋ฉ”๋‰ด

DOing

[Querydsl] Join ๋ณธ๋ฌธ

JPA

[Querydsl] Join

mangdo 2021. 8. 4. 23:13

๐Ÿ€ Join

: join(์กฐ์ธ ๋Œ€์ƒ, ๋ณ„์นญ) ๊ณผ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ€์ง€๋ฉด ๋œ๋‹ค. 

: ์ง€์›ํ•˜๋Š” Join์˜ ์ข…๋ฅ˜

  • join() , innerJoin() : ๋‚ด๋ถ€ ์กฐ์ธ(inner join)
  • leftJoin() : left ์™ธ๋ถ€ ์กฐ์ธ(left outer join)
  • rightJoin() : rigth ์™ธ๋ถ€ ์กฐ์ธ(rigth outer join)

 

๐Ÿ€ ๋‚ด๋ถ€ ์กฐ์ธ(inner join)

 ๋‚ด๋ถ€ ์กฐ์ธ(inner join)์˜ ๊ฒฝ์šฐ, on ์ ˆ์„ ํ™œ์šฉํ•ด ์กฐ์ธ ๋Œ€์ƒ์„ ํ•„ํ„ฐ๋ง ํ•˜๋Š” ๊ฒƒ๊ณผwhere ์ ˆ์—์„œ ํ•„ํ„ฐ๋ง ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ธฐ๋Šฅ์ด ๋™์ผํ•˜๋‹ค!!!

    @Test
    public void join() throws Exception {
        QMember member = QMember.member;
        QTeam team = QTeam.team;
        List<Member> result = queryFactory
                .selectFrom(member)
                .join(member.team, team)
                //.on(team.name.eq("teamA"))
                .where(team.name.eq("teamA"))
                .fetch();
        assertThat(result)
                .extracting("username")
                .containsExactly("member1", "member2");
    }

 

๐Ÿ€ ์—ฐ๊ด€ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ํ•„๋“œ๋กœ ์กฐ์ธ

1. ๋‚ด๋ถ€ ์กฐ์ธ (์„ธํƒ€ ์กฐ์ธ)

: ์—ฐ๊ด€๊ด€๊ณ„๊ฐ€ ์—†๋Š” ํ•„๋“œ๋กœ๋„ ์กฐ์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

: from ์ ˆ์— ์—ฌ๋Ÿฌ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์„ ํƒํ•ด์„œ ์„ธํƒ€ ์กฐ์ธ

: cross join์œผ๋กœ ์ฟผ๋ฆฌ๊ฐ€ ๋‚˜๊ฐ„๋‹ค.

    @Test
    public void theta_join() throws Exception {
        em.persist(new Member("teamA"));
        em.persist(new Member("teamB"));
        List<Member> result = queryFactory
                                  .select(member)
                                  .from(member, team)
                                  .where(member.username.eq(team.name))
                                  .fetch();
        assertThat(result)
                .extracting("username")
                .containsExactly("teamA", "teamB");
    }

 

2. ์™ธ๋ถ€ ์กฐ์ธ

: ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ leftJoin์— member.team์ด ์•„๋‹Œ team์ด ๋“ค์–ด๊ฐ”๋Š” ์ ์ด๋‹ค.

  ๋•Œ๋ฌธ์— leftJoin(member.team)์—์„œ๋Š” ์ฟผ๋ฆฌ๊ฐ€ on member.team_id=team.id๊ฐ€ ์ถ”๊ฐ€๋˜์ง€๋งŒ leftJoin(team)์—์„œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค.

@Test
public void join_on_no_relation() throws Exception {
      em.persist(new Member("teamA"));
      em.persist(new Member("teamB"));
      
      List<Tuple> result = queryFactory
                              .select(member, team)
                              .from(member)
                              .leftJoin(team).on(member.username.eq(team.name))
                              .fetch();
      for (Tuple tuple : result) {
      	System.out.println("t=" + tuple);
      }
}

 

๐Ÿ€ ํŒจ์น˜ ์กฐ์ธ

: SQL์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์•„๋‹ˆ๋‹ค. 

: SQL์กฐ์ธ์„ ํ™œ์šฉํ•ด์„œ ์—ฐ๊ด€๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ SQL ํ•œ๋ฒˆ์— ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ์ฃผ๋กœ ์„ฑ๋Šฅ ์ตœ์ ํ™”์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

: ํ˜„์žฌ ์˜ˆ์‹œ์—์„œ๋Š” member๋ฅผ ์กฐํšŒํ•  ๋•Œ member์— ์—ฐ๊ด€๋œ team์„ ๋ชจ๋‘ ๋‹ค ๋Œ๊ณ  ์˜จ๋‹ค.

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