티스토리 뷰

 

인프런 - 김영한 JPA 강의 중

 

인프런 - 김영한 JPA 강의 중

 

 

package jpabook.jpashop.domain;

import javax.persistence.*;

@Entity
public class Child {
    @Id
    @GeneratedValue
    private String id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    private Parent parent;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }
}

 

package jpabook.jpashop.domain;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Parent {

        @Id @GeneratedValue
        private String id;

        private String name;

        @OneToMany(mappedBy = "parent" , cascade = CascadeType.ALL)
        List<Child> childList = new ArrayList<>();

        public void addChild(Child child){
                childList.add(child);
                child.setParent(this);
        }
        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public List<Child> getChildList() {
                return childList;
        }

        public void setChildList(List<Child> childList) {
                this.childList = childList;
        }
}

 

 

            Child child1 = new Child();
            Child child2 = new Child();

            Parent parent = new Parent();

            parent.addChild(child1);
            parent.addChild(child2);

            em.persist(parent);
            tx.commit();

 

해당 코드와 같이 작성을 하면 원래는 parent만 저장 되던 것이 

@OneToMany(mappedBy = "parent" , cascade = CascadeType.ALL)

로 인해 child까지 저장이 된다.

 

인프런 - 김영한 JPA 강의 중

 

언제 쓰느냐?

- 정말 하나의 부모가 자식들을 관리 할 때에 의미가 있다. 게시판이나 첨부파일 등 소유자가 1개 일때에만 사용한다.

 

 

인프런 - 김영한 JPA 강의 중

 

@OneToMany(mappedBy = "parent" , cascade = CascadeType.ALL, orphanRemoval = true)
List<Child> childList = new ArrayList<>();

orphanRemoval를 true로 두고 사용할 경우에는 

Parent findParent = em.find(Parent.class, parent.getId());
findParent.getChildList().remove(0);

delete 쿼리를 날리게 된다.

 

 

**참조하는 곳이 하나일 때에만 사용한다!

 

            Child child1 = new Child();
            Child child2 = new Child();

            Parent parent = new Parent();

            parent.addChild(child1);
            parent.addChild(child2);

            em.persist(parent);
            em.flush();
            em.close();

            Parent findParent = em.find(Parent.class, parent.getId());
            findParent.getChildList().remove(0);

            em.remove(parent);

 

orphanRemoval를 사용할 경우 부모만 삭제된다면 자식들 까지 삭제된다.

cascade = CASCADETYPE.REMOVE와 같은 동작을 하는데

 

사실 casecade = CASCADETYPE.ALL을 사용해도 같은 동작을 한다.

 

인프런 - 김영한 JPA 강의

 

 

'dev_공부일지 > JPA 기본' 카테고리의 다른 글

임베디드 타입  (1) 2024.02.06
값 타입  (0) 2024.02.06
즉시로딩과 지연로딩  (0) 2024.02.05
@MappedSuperclass  (0) 2024.02.03
상속관계 매핑  (0) 2024.02.03
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함