JAVA/JPA

(JPA) 영속성 전이, 고아 객체

ri5 2021. 7. 6. 14:58

영속성 전이: CASCADE


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

• 예: 부모 엔티티를 저장할 때 자식 엔티티도 함께 저장.

 

영속성 전이: 저장

- Parent

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

    private String name;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST)
    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

Parent parent = new Parent();

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

// parent의 child List에 추가
parent.addChild(child1);
parent.addChild(child2);

// parent와 종속된 childList의 있는 child도 같이 저장됨
em.persist(parent);

영속성 전이의 주의점

• 영속성 전이는 연관관계를 매핑하는 것과 아무 관련이 없음

• 엔티티를 영속화할 때 연관된 엔티티도 함께 영속화하는 편리함 을 제공할 뿐

• 예) 한 게시글의 첨부 파일, 주문 하나의 상품목록

 

CASCADE의 종류

• ALL: 모두 적용

• PERSIST: 영속

• REMOVE: 삭제

• MERGE: 병합

• REFRESH: REFRESH

• DETACH: DETACH

※ 실무에서는 표시된 3개를 주로 사용함

 

고아 객체


• 고아 객체 제거: 부모 엔티티와 연관관계가 끊어진 자식 엔티티 를 자동으로 삭제

• orphanRemoval = true 

 

example

- Parent

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

    private String name;
	// orphanRemoval 설정
    @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);
    }
}

- Main

Parent parent = new Parent();

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

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);
// DELETE FROM CHILD WHERE ID=? 쿼리가 나간다.
em.commit();

 

고아 객체의 주의점

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

• 참조하는 곳이 하나일 때 사용해야함!

• 특정 엔티티가 개인 소유할 때 사용

• @OneToOne, @OneToMany만 가능

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

 

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

• CascadeType.ALL + orphanRemovel=true

• 스스로 생명주기를 관리하는 엔티티는 em.persist()로 영속화, em.remove()로 제거

• 두 옵션을 모두 활성화 하면 부모 엔티티를 통해서 자식의 생명 주기를 관리할 수 있음

도메인 주도 설계(DDD)의 Aggregate Root개념을 구현할 때 유