본문 바로가기
Java/JPA

[JPA_Basic] Cascade

by HJ0216 2023. 9. 11.

이 글은 김영한의 [자바 ORM 표준 JPA 프로그래밍 - 기본편] 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Java

- DB: H2 Database

- IDE: IntelliJ

 

 

⭐ 영속성 전이: CASCADE

특정 Entity를 영속 상태로 만들 때, 연관된 Entity도 함께 영속상태로 만들고 싶을 때 사용

 

⌨️ 코드

Entity 객체

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
@Entity
public class Parent {
 
    @Id @GeneratedValue
    private long id;
 
    private String name;
 
    @OneToMany(mappedBy = "parent")
    private List<Child> children = new ArrayList<>();
 
    public void addChild(Child child){
        children.add(child);
        child.setParent(this);
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public List<Child> getChildren() {
        return children;
    }
 
    public void setChildren(List<Child> children) {
        this.children = children;
    }
}
 
 

main method

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
public class Main {
    public static void main(String[] args) {
 
        // 생략
 
        try {
 
            Child child1 = new Child();
            Child child2 = new Child();
 
            Parent parent = new Parent();
            parent.addChild(child1);
            parent.addChild(child2);
 
            em.persist(parent);
 
            em.flush();
            em.clear();
 
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            e.printStackTrace();
        } finally {
            em.close();
        }
 
 
        emf.close();
 
    }
 
}
 
 

 

🖨️발생한 쿼리

1
2
3
4
5
6
7
8
9
Hibernate: 
    /* insert jpa_basic.Parent
        */ insert 
        into
            Parent
            (name, id) 
        values
            (?, ?)
 
 

Child 객체는 저장되지 않고, Parent 객체만 저장됨 

 

⭐CascadeType.All 적용 후,

Entity 객체

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
@Entity
public class Parent {
 
    @Id @GeneratedValue
    private long id;
 
    private String name;
    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private List<Child> children = new ArrayList<>();
 
    public void addChild(Child child){
        children.add(child);
        child.setParent(this);
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public List<Child> getChildren() {
        return children;
    }
 
    public void setChildren(List<Child> children) {
        this.children = children;
    }
}
 
 

 

🖨️발생한 쿼리

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
Hibernate: 
    /* insert jpa_basic.Parent
        */ insert 
        into
            Parent
            (name, id) 
        values
            (?, ?)
 
Hibernate: 
    /* insert jpa_basic.Child
        */ insert 
        into
            Child
            (name, parent_id, id) 
        values
            (?, ?, ?)
 
Hibernate: 
    /* insert jpa_basic.Child
        */ insert 
        into
            Child
            (name, parent_id, id) 
        values
            (?, ?, ?)
 
 

Parent 객체와 함께 Child 객체도 저장

 

- CASCADE 종류(일부)

    - ALL: 모두 적용

    - PERSIST: 영속

 

🚨 부모 객체로 인한 변경 시, 다른 객체에 영향을 미칠 수 있으므로 CASCADE 사용 시, 참조하는 곳이 하나일 때만 사용해야함

 

'Java > JPA' 카테고리의 다른 글

[JPA_Basic] 값 타입 컬렉션  (0) 2023.09.19
[JPA_Basic] 값 타입과 불변 객체  (0) 2023.09.14
[JPA_Basic] N+1 문제  (0) 2023.09.10
[JPA_Basic] Proxy  (0) 2023.09.08
[JPA_Basic] @MappedSuperclass  (0) 2023.09.05