Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 김영한
- 스프링
- 백준
- JDBC
- springdatajpa
- 스프링 핵심 원리
- QueryDSL
- Servlet
- jpa
- db
- kotlin
- 인프런
- 자바
- Greedy
- Thymeleaf
- java
- 스프링 핵심 기능
- JPQL
- Spring Boot
- http
- 알고리즘
- pointcut
- Exception
- Proxy
- 그리디
- AOP
- SpringBoot
- transaction
- spring
- Android
Archives
- Today
- Total
개발자되기 프로젝트
JPQL JOIN 본문
1. Join
- 객체 중심.
- 내부 조인 : INNER JOIN,
- inner는 생략 가능.
- 멤버는 있는데 팀이 없으면 결과 안나옴.
- SELECT m FROM Member m [INNER] JPOIN m.team t
- 외부 조인 : LEFT OUTER or LEFT
- outer는 생략 가능.
- 팀이 없어도 팀 데이터 NULL로 넣고 멤버는 조회됨.
- SELECT m FROM Member m LEFT [OUTER] JOIN m.team t
- 세타 조인
- 연관관계가 없는 두 엔티티를 막 비교해보고 싶을 때 사용.
- select count(m) from Member m, Team t where m.username = t.name
2. 내부 조인
//member 와 team을 inner join함. 이제 team을 쓸 수 있음.
String query = "select m from Member m inner join m.team t";
List<Member> resultList = em.createQuery(query, Member.class)
.getResultList();
System.out.println("resultList.size() = " + resultList.size());
for (Member member1 : resultList) {
System.out.println("member1.toString() = " + member1.toString());
}
Hibernate:
/* 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_.TEAM_ID
3. 외부 조인
//member 와 team을 inner join함. 이제 team을 쓸 수 있음.
String query = "select m from Member m left outer join m.team t";
List<Member> resultList = em.createQuery(query, Member.class)
.getResultList();
System.out.println("resultList.size() = " + resultList.size());
for (Member member1 : resultList) {
System.out.println("member1.toString() = " + member1.toString());
}
Hibernate:
/* 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_.TEAM_ID
3. 세타 조인
String query = "select m from Member m, Team t where m.username = t.name";
List<Member> resultList = em.createQuery(query, Member.class)
.getResultList();
System.out.println("resultList.size() = " + resultList.size());
for (Member member1 : resultList) {
System.out.println("member1.toString() = " + member1.toString());
}
Hibernate:
/* 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
4. join - ON 절
- ON절을 활용한 조인
- 조인 대상을 미리 필터링
- 연관관계 없는 엔티티 외부 조인
- 조인 대상 필터링
- 예를들어 회원과 팀을 조인하면서 팀이름이 A인 팀만 조인
- JPQL
member m에서 team을 조인하는데 team 이름이 'A'인 경우만.
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 join m.team t on t.name = 'team'";
- 조인 조건문 안에 필터링 조건 추가됨.
Hibernate: /* select m from Member m left join m.team t on t.name = 'team' */ 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_.TEAM_ID and ( team1_.name='team' )
- 연관관계 없는 엔티티 외부 조인
- 회원의 이름과 팀의 이름이 같은 대상을 외부 조인
- 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 outer join Team t on m.username = t.name";
-
Hibernate: /* select m from Member m left outer 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 )
5. GitHub : 210816 JOIN
'인프런 > [인프런] 자바ORM 표준 JPA 프로그래밍' 카테고리의 다른 글
JPQL 타입 표현 (0) | 2021.08.16 |
---|---|
서브쿼리 (0) | 2021.08.16 |
JPQL 페이징 API (0) | 2021.08.16 |
JPQL 프로젝션 (0) | 2021.08.16 |
JPQL 기본 문법과 기능 (0) | 2021.08.15 |
Comments