이 글은 김영한의 [자바 ORM 표준 JPA 프로그래밍 - 기본편]을 수강하며 정리한 글입니다.
👉 기본 환경
- Language: Java
- DB: H2 Database
- IDE: IntelliJ
Bulk 연산
⌨️ 코드
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
50
51
52
53
54
55
|
public class Main {
public static void main(String[] args) {
// 생략
try {
Team teamA = new Team();
teamA.setName("TeamA");
em.persist(teamA);
Team teamB = new Team();
teamB.setName("TeamB");
em.persist(teamB);
Member memberA = new Member();
memberA.setName("MemberA");
memberA.setTeam(teamA);
em.persist(memberA);
Member memberB = new Member();
memberB.setName("memberB");
memberB.setTeam(teamA);
em.persist(memberB);
Member memberC = new Member();
memberC.setName("memberC");
memberC.setTeam(teamB);
em.persist(memberC);
String query = "update Member m set m.age = 20";
int resultCnt = em.createQuery(query)
.executeUpdate();
System.out.println("ResultCnt: " + resultCnt);
Member findMember = em.find(Member.class, memberA.getId());
System.out.println("memberA.getAge(): " + findMember.getAge());
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
- executeUpdate()
- 벌크 연산은 영속성 컨텍스트를 무시하고 데이터베이스에 직접 쿼리 전송
- DB: memberA.getAge()는 20
- 영속성 Context: memberA.getAge()는 20이 아닌 0으로 조회
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
|
Hibernate:
/* update
Member m
set
m.age = 20 */ update
Member
set
age=20
ResultCnt: 3
memberA.getAge(): 0
|
⭐ 벌크 연산 후, 영속성 컨텍스트 초기화
⌨️ 코드
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
50
51
52
53
54
55
56
57
|
public class Main {
public static void main(String[] args) {
// 생략
try {
Team teamA = new Team();
teamA.setName("TeamA");
em.persist(teamA);
Team teamB = new Team();
teamB.setName("TeamB");
em.persist(teamB);
Member memberA = new Member();
memberA.setName("MemberA");
memberA.setTeam(teamA);
em.persist(memberA);
Member memberB = new Member();
memberB.setName("memberB");
memberB.setTeam(teamA);
em.persist(memberB);
Member memberC = new Member();
memberC.setName("memberC");
memberC.setTeam(teamB);
em.persist(memberC);
String query = "update Member m set m.age = 20";
int resultCnt = em.createQuery(query)
.executeUpdate();
System.out.println("ResultCnt: " + resultCnt);
em.clear();
Member findMember = em.find(Member.class, memberA.getId());
System.out.println("memberA.getAge(): " + findMember.getAge());
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Hibernate:
/* update
Member m
set
m.age = 20 */ update
Member
set
age=20
ResultCnt: 3
Hibernate:
select
member0_.id as id1_0_0_,
member0_.age as age2_0_0_,
member0_.name as name3_0_0_,
member0_.TEAM_ID as team_id5_0_0_,
member0_.type as type4_0_0_
from
Member member0_
where
member0_.id=?
memberA.getAge(): 20
|
새로운 select query 발생
'Java > JPA' 카테고리의 다른 글
[JPA_Basic] Fetch Join 2 (0) | 2023.09.28 |
---|---|
[JPA_Basic] Fetch Join (0) | 2023.09.27 |
[JPA_Basic] 경로 표현식 (0) | 2023.09.26 |
[JPA_Basic] JPQL 타입 표현과 기타식 (1) | 2023.09.25 |
[JPA_Basic] 조인 (0) | 2023.09.23 |