@SpringBootTest@TransactionalpublicclassQuerydslBasicTest { /** * 팀A에 소속된 모든 회원 */ @Testpublicvoidjoin() throwsException {QMember member =QMember.member;QTeam team =QTeam.team;List<Member> result = queryFactory.selectFrom(member)// 멤버와 팀을 조인한다..join(member.team, team).where(team.name.eq("teamA")).fetch();assertThat(result).extracting("username") }}
join(조인 대상, 별칭으로 사용할 Q타입)
select member1from Member member1inner join member1.team as teamwhere team.name = ?1
join(), innerJoin()
inner join
leftJoin()
left outer join
rightJoin()
right outer join
JPQL의 on과 성능 최적화를 위한 fetch join도 제공한다.
세타 조인
연관 관계가 없는 필드로 조인한다.
@SpringBootTest@TransactionalpublicclassQuerydslBasicTest { /** * 세타 조인(연관 관계가 없는 필드로 조인) * 회원의 이름이 팀 이름과 같은 회원 조회 */ @Testpublicvoidtheta_join() throwsException {em.persist(newMember("teamA"));em.persist(newMember("teamB"));List<Member> result = queryFactory.select(member)// 일반 조인은 멤버와 연관이 있는 팀을 지정했지만// 세타 조인은 그냥 필요한 엔티티를 나열한다..from(member, team).where(member.username.eq(team.name)).fetch();assertThat(result).extracting("username").containsExactly("teamA","teamB"); }}
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_idwhere member0_.username = ?