[JPA] JPQL - 조인
17 Aug 2022해당 포스트는 인프런 김영한님의 자바 ORM 표준 JPA 프로그래밍 - 기본편 을 듣고 정리한 글입니다.
조인
종류
- 내부 조인:
SELECT m FROM Member m [INNER] JOIN m.team t
- 외부 조인:
SELECT m FROM Member m LEFT [OUTER] JOIN m.team t
- 세타 조인:
select count(m) from Member m, Team t where m.username = t.name
세타 조인은 연관관계가 없는 엔티티를 카티션 프로덕트로 모두 가져오는 조인
예시
inner join
Team team = new Team();
team.setName("teamA");
entityManager.persist(team);
Member member = new Member();
member.setUsername("member");
member.setAge(10);
member.setTeam(team);
entityManager.persist(member);
/* inner는 생략이 가능하다. */
/* String query = "select m from Member m inner join m.team t"; */
String query = "select m from Member m join m.team t";
List<Member> result = entityManager.createQuery(query, Member.class)
.getResultList();
/* select
m
from
Member m
inner join
m.team t */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id4_0_,
member0_.username as username3_0_
from
Member member0_
inner join
Team team1_
on member0_.TEAM_ID=team1_.id
left outer join
/* outer을 생략이 가능하다. */
/* String query = "select m from Member m left outer join m.team t"; */
String query = "select m from Member m left join m.team t";
List<Member> result = entityManager.createQuery(query, Member.class)
.getResultList();
/* select
m
from
Member m
left outer join
m.team t */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id4_0_,
member0_.username as username3_0_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
세타 조인
String query = "select m from Member m, Team t where m.username = t.name";
List<Member> result = entityManager.createQuery(query, Member.class)
.getResultList();
/* select
m
from
Member m,
Team t
where
m.username = t.name */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id4_0_,
member0_.username as username3_0_
from
Member member0_ cross
join
Team team1_
where
member0_.username=team1_.name
조인 - ON절
- ON절을 활용한 조인 (JPA 2.1부터 지원 하지만 대게 2.1 이상을 사용한다..)
1. 조인 대상 필터링
- 예) 회원과 팀을 조인하면서, 팀 이름이 A인 팀만 조인
JPQL: SELECT m, t FROM Member m LEFT JOIN m.team t on t.name = ‘A’ SQL: SELECT m., t. FROM Member m LEFT JOIN Team t ON m.TEAM_ID=t.id and t.name=’A’
String query = "select m from Member m left outer join m.team t on t.name = 'teamA'";
List<Member> result = entityManager.createQuery(query, Member.class)
.getResultList();
/* select
m
from
Member m
left outer join
m.team t
on t.name = 'teamA' */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id4_0_,
member0_.username as username3_0_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
and (
team1_.name='teamA'
)
and 조건에 추가로 teamA라는 조건식이 추가되는 것을 확인 할 수있다.
2. 연관관계 없는 엔티티 외부 조인
- 예) 회원의 이름과 팀의 이름이 같은 대상 외부 조인
JPQL: SELECT m, t FROM Member m LEFT JOIN Team t on m.username = t.name SQL: SELECT m., t. FROM Member m LEFT JOIN Team t ON m.username = t.name
String query = "select m from Member m left join Team t on m.username = t.name";
List<Member> result = entityManager.createQuery(query, Member.class)
.getResultList();
/* select
m
from
Member m
left join
Team t
on m.username = t.name */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.TEAM_ID as team_id4_0_,
member0_.username as username3_0_
from
Member member0_
left outer join
Team team1_
on (
member0_.username=team1_.name
)
on 절에 추가한 조건식이 들어가는 것을 확인 할 수 있다.