Querydsl - 집합, group와 join

청포도봉봉이·2023년 3월 21일
0

Spring

목록 보기
5/35
post-thumbnail

집합

@Test
    public void aggregation() {
        List<Tuple> result = queryFactory
                .select(
                        member.count(),
                        member.age.sum(),
                        member.age.avg(),
                        member.age.max(),
                        member.age.min()
                )
                .from(member)
                .fetch();

        Tuple tuple = result.get(0);

        assertThat(tuple.get(member.count())).isEqualTo(4);
        assertThat(tuple.get(member.age.sum())).isEqualTo(100);
        assertThat(tuple.get(member.age.avg())).isEqualTo(25);
        assertThat(tuple.get(member.age.max())).isEqualTo(40);
        assertThat(tuple.get(member.age.min())).isEqualTo(10);
    }
  • count, sum, avg, max, min 등 지원



Group

/**
    *   팀의 이름과 각 팀의 평균 연령 구하기
    *
    */
    @Test
    public void group() {
        List<Tuple> result = queryFactory
                .select(
                        team.name,
                        member.age.avg()
                )
                .from(member)
                .join(member.team, team)
                .groupBy(member.team.name)
                .fetch();

        for (Tuple tuple : result) {
            System.out.println("tuple = " + tuple);
        }

        Tuple teamA = result.get(0);
        Tuple teamB = result.get(1);

        assertThat(teamA.get(team.name)).isEqualTo("teamA");
        assertThat(teamB.get(team.name)).isEqualTo("teamB");
        assertThat(teamA.get(member.age.avg())).isEqualTo(15);
        assertThat(teamB.get(member.age.avg())).isEqualTo(35);
    }
  • .join()에서 member.team, team 매핑할 관계를 나열 후 join
  • .groupBy() 지원



join

/**
    teamA에 소속된 모든 회원을 찾자
     */
    @Test
    public void join() {
        List<Member> result = queryFactory
                .selectFrom(member)
                .leftJoin(member.team, team)
                .where(member.team.name.eq("teamA"))
                .fetch();

        Member member1 = result.get(0);
        Member member2 = result.get(1);
        assertThat(member1.getUsername()).isEqualTo("member1");
        assertThat(member2.getAge()).isEqualTo(20);

        assertThat(result)
                .extracting("username")
                .containsExactly("member1", "member2");
    }
  • leftJoin, join, rightJoin 등 지원



theta 조인

    /**
     * 세타 조인
     * 회원 이름이 팀 이름과 같은 회원 조회
     */
    @Test
    public void theta_join() {
        em.persist(new Member("teamA"));
        em.persist(new Member("teamB"));
        em.persist(new Member("teamC"));

        List<Member> result = queryFactory
                .select(member)
                .from(member, team)
                .where(member.username.eq(team.name))
                .fetch();

        assertThat(result)
                .extracting("username")
                .containsExactly("teamA", "teamB");
    }
  • theta 조인이란 연관관계가 없는 테이블끼리도 조인 가능
  • from절에 join할 관계를 나열한다.
profile
서버 백엔드 개발자

0개의 댓글