집합
@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
@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할 관계를 나열한다.