이 글은 김영한의 [자바 ORM 표준 JPA 프로그래밍 - 기본편]을 수강하며 정리한 글입니다.
👉 기본 환경
- Language: Java
- DB: H2 Database
- IDE: IntelliJ
Inner Join
⌨️ 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m join m.team t";
List<Member> members = em.createQuery(query, Member.class)
.getResultList();
System.out.println("members.size: " + members.size());
for(Member m : members)
System.out.println("member: " + m);
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Hibernate:
/* select
m
from
Member m
inner join
m.team t */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.name as name3_0_,
member0_.TEAM_ID as team_id4_0_
from
Member member0_
inner join
Team team1_
on member0_.TEAM_ID=team1_.id
|
Outer Join
⌨️ 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m left join m.team t";
List<Member> members = em.createQuery(query, Member.class)
.getResultList();
System.out.println("members.size: " + members.size());
for(Member m : members)
System.out.println("member: " + m);
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Hibernate:
/* select
m
from
Member m
left join
m.team t */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.name as name3_0_,
member0_.TEAM_ID as team_id4_0_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
|
Theta Join
⌨️ 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m, Team t where m.name=t.name";
List<Member> members = em.createQuery(query, Member.class)
.getResultList();
System.out.println("members.size: " + members.size());
for(Member m : members)
System.out.println("member: " + m);
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Hibernate:
/* select
m
from
Member m,
Team t
where
m.name=t.name */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.name as name3_0_,
member0_.TEAM_ID as team_id4_0_
from
Member member0_ cross
join
Team team1_
where
member0_.name=team1_.name
|
ON 조건절 활용
1. 조인 대상 필터링
⌨️ 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m join m.team t on t.name= :name";
List<Member> members = em.createQuery(query, Member.class)
.setParameter("name", "Team")
.getResultList();
System.out.println("members.size: " + members.size());
for(Member m : members)
System.out.println("member: " + m);
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Hibernate:
/* select
m
from
Member m
join
m.team t
on t.name= :name */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.name as name3_0_,
member0_.TEAM_ID as team_id4_0_
from
Member member0_
inner join
Team team1_
on member0_.TEAM_ID=team1_.id
and (
team1_.name=?
)
|
⭐ 필터링 후 조인 가능
2. 연관관계 없는 엔티티 외부 조인
⌨️ 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
em.persist(member);
em.flush();
em.clear();
String query = "select m from Member m left join m.team t on t.name=m.name";
List<Member> members = em.createQuery(query, Member.class)
.getResultList();
System.out.println("members.size: " + members.size());
for(Member m : members)
System.out.println("member: " + m);
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Hibernate:
/* select
m
from
Member m
left join
m.team t
on t.name=m.name */ select
member0_.id as id1_0_,
member0_.age as age2_0_,
member0_.name as name3_0_,
member0_.TEAM_ID as team_id4_0_
from
Member member0_
left outer join
Team team1_
on member0_.TEAM_ID=team1_.id
and (
team1_.name=member0_.name
)
|
⭐ 연관관계 없이도 외부 조인 가능
'Java > JPA' 카테고리의 다른 글
[JPA_Basic] 경로 표현식 (0) | 2023.09.26 |
---|---|
[JPA_Basic] JPQL 타입 표현과 기타식 (1) | 2023.09.25 |
[JPA_Basic] 페이징 (0) | 2023.09.23 |
[JPA_Basic] 프로젝션 - 여러 값 조회 (1) | 2023.09.22 |
[JPA_Basic] 값 타입 컬렉션 (0) | 2023.09.19 |