[Spring JPA] 영속성 전이(Cascade)와 고아객체

2021. 6. 28. 18:53카테고리 없음

영속성 전이 : CASCADE

  • 특정 엔티티를 영속 상태로 만들 때 연관된 엔티티도 함께 영속 상태로 만들고 싶을 때

2021-06-28-5-59-22.png

Parent

@Entity
public class Parent {
    @Id @GeneratedValue
    private Long id;

    private String name;

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

    public void addChild(Child child) {
        childList.add(child);
        child.setParent(this);
    }
}

Child

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

    private String name;

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

Main

  //원래 저장할 때는 이렇게 해야한다.
    Child child1 = new Child();
  Child child2 = new Child();

  Parent parent = new Parent();
  parent.addChild(child1);
  parent.addChild(child2);

  em.persist(parent);
  em.persist(child1);
  em.persist(child2);


    //CASCADE를 사용할 때
    Child child1 = new Child();
  Child child2 = new Child();

  Parent parent = new Parent();
  parent.addChild(child1);
  parent.addChild(child2);

  em.persist(parent);

CASCADE를 사용하지 않을 때

Hibernate: 
    /* insert hellojpa.Parent
        */ insert 
        into
            Parent
            (name, id) 
        values
            (?, ?)
Hibernate: 
    /* insert hellojpa.Child
        */ insert 
        into
            Child
            (name, parent_id, id) 
        values
            (?, ?, ?)
Hibernate: 
    /* insert hellojpa.Child
        */ insert 
        into
            Child
            (name, parent_id, id) 
        values
            (?, ?, ?)

CASCADE를 사용할 때

Hibernate: 
    /* insert hellojpa.Parent
        */ insert 
        into
            Parent
            (name, id) 
        values
            (?, ?)
Hibernate: 
    /* insert hellojpa.Child
        */ insert 
        into
            Child
            (name, parent_id, id) 
        values
            (?, ?, ?)
Hibernate: 
    /* insert hellojpa.Child
        */ insert 
        into
            Child
            (name, parent_id, id) 
        values
            (?, ?, ?)
  • 같은 결과가 나온다!!!

2021-06-28-6-03-08.png


주의사항!

  • 영속성 전이는 연관관계를 매핑하는 것과 아무~ 관련이 없다.
  • 엔티티를 영속화할 때 연관된 엔티티도 함께 영속화하는 편리함을 제공할 뿐!!

종류

  • ALL : 모두 적용
  • PERSIST : 영속
  • REMOVE : 삭제
  • MERGE : 병합
  • REFRESH : REFRESH
  • DETACH : DETACH

고아객체

  • 고아 객체 제거 : 부모 엔티티와 연관관계가 끊어진 자식 엔티티를 자동으로 삭제
  • orphanRemoval = true
  • Parent parent1 = em.find(Parent.class, id);
    parent1.getChildren().remove(0); //자식 엔티티를 컬렉션에서 제거
  • DELETE FROM CHILD WHERE ID = ?

코드

@Entity
public class Parent {
    @Id @GeneratedValue
    private Long id;

    private String name;

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

    public void addChild(Child child) {
        childList.add(child);
        child.setParent(this);
    }
}
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();

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

결과

Hibernate: 
    /* delete hellojpa.Child */ delete 
        from
            Child 
        where
            id=?

고아 객체 - 주의사항

  • 참조가 제건된 엔티티는 다른 곳에서 참조하지 않는 고아 객체로 보고 삭제하는 기능

  • 참조하는 곳이 하나일 때 사용해야한다.!!!!!!!!!!!

    • 만약 Child를 Parent만 아는게 아니라 Member도 알고 다른 객체도 알고 있게 되면 사용하면 안된다.!
      이 부분은 CASCADE도 동일함!
  • 특정 엔티티가 개인 소유할 때 사용(위에 말과 동일하다.)

  • @OneToOne, @OneToMany만 가능

  • 참고 : 개념적으로 부모를 제거하면 자식은 고아가 된다. 따라서 고아 객체 제거 기능을 활성화 하면, 부모를 제거할 때 자식도 함께 제거가 된다. 이것은 CascadeType.REMOVE처럼 동작한다.

    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();
    
    Parent findParent = em.find(Parent.class, parent.getId());
    em.remove(findParent);// 이 부분를 보면 parent를 삭제한다. 그리고 parent 엔티티에 orphanRemoval = true를 활성화 시켰다!

    결과

    Hibernate: 
        /* delete hellojpa.Child */ delete 
            from
                Child 
            where
                id=?
    Hibernate: 
        /* delete hellojpa.Child */ delete 
            from
                Child 
            where
                id=?
    Hibernate: 
        /* delete hellojpa.Parent */ delete 
            from
                Parent 
            where
                id=?
    
    • 부모도 지워지면서 자식들도 고아객체가 되면서 삭제가 된다.

영속성 전이 + 고아 객체, 생명주기

  • CascadeType.ALL + orphanRemoval = true
  • 스스로 생명주기를 관리하는 엔티티는 em.persist()로 영속화, em.remove()로 제거
  • 두 옵션을 모두 활성화 하면 부모 엔티티를 통해서 자식의 생명주기를 관리할 수 있다.
    • 즉, findParent.getChildList().remove(0); 이 코드처럼 부모를 통해서 자식을 관리할 수 있다!!

참고자료