공부 내용을 정리하고 앞으로의 학습에 이해를 돕기 위해 작성합니다.
영속성 전이
영속성 전이:CASCADE
- 특정 엔티티를 영속 상태로 만들 때 연관된 엔티티도 함께 영속 상태로 만들기 위해 사용된다.
- ex) 부모 엔티티를 저장할 때 자식 엔티티도 함께 저장한다.
우선 영속성 전이가 아닌 예를 보자.
@Entity
public class Parent {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "parent")
private List<Child> childList = new ArrayList<>();
public void addChild(Child child) {
childList.add(child);
child.setParent(this);
}
}
=========================================================
@Entity
public class Child {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "parent_id")
private Parent parent;
}
public class JpaMain {
//생략
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);
tx.commit();
}
- 이렇게 코드를 짜면 persist를 3번이나 호출해야 한다.
영속성 전이를 사용하면 parent를 persist 할 때 child는 자동으로 관리할 수 있다.
@Entity
public class Parent {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<Child> childList = new ArrayList<>();
public void addChild(Child child) {
childList.add(child);
child.setParent(this);
}
...
}
public class JpaMain {
...생략...
Child child1 = new Child();
Child child2 = new Child();
Parent parent = new Parent();
parent.addChild(child1);
parent.addChild(child2);
em.persist(parent);
tx.commit();
}
- parent만 영속화 했지만 자식들도 같이 영속화되는 것을 확인할 수 있다.
- DB에도 PARENT_ID 1번으로 입력된 것을 볼 수 있다.
그림으로 보면 다음과 같다.
영속성 전이(CASCADE) - 주의
- 영속성 전이는 연관관계를 매핑하는 것과 아무 관련이 없다.
- 엔티티를 영속화할 때 연관된 엔티티도 함께 영속화하는 편리함을 제공할 뿐이다.
CASCADE의 종류
- ALL: 모두 적용 (life cycle 다 맞춰야 할 때)
- PERSIST: 영속 (저장할 때만 life cycle 맞출 때)
- REMOVE : 삭제
- MERGE : 병합
- REFRESH
고아 객체
- 부모 엔티티와 연관관계가 끊어진 자식 엔티티를 자동으로 삭제하는 기능이다.
- 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<>();
}
- orphanRemoval = true로 설정한다.
public class JpaMain {
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();
Parent findParent = em.find(Parent.class, parent.getId());
findParent.getChildList().remove(0);
tx.commit();
}
}
}
- 0번 자식을 Parent 컬렉션에서 삭제 했기 때문에 0번 자식은 자동 삭제 된다.
- 0번 자식이 삭제 되었다.
주의
- 참조가 제거된 엔티티는 다른 곳에서 참조하지 않는 고아 객체로 보고 삭제하는 기능이다.
- 참조하는 곳이 하나일 때 사용해야 한다.
- 특정 엔티티가 개인 소유할 때 사용한다.
- @OneToOne, @OneToMany만 가능하다.
- 부모를 제거하면 자식은 고아가 되므로, 고아 객체 제거 기능을 활성화하면 부모를 제거할 때 자식도 함께 제거된다.
- 이는 CascadeType.REMOVE처럼 동작한다.
Parent findParent = em.find(Parent.class, parent.getId());
em.remove(findParent);
- 부모를 삭제하면 자식도 다 삭제된다.
영속성 전이 + 고아 객체, 생명주기
- CascadeType.ALL + orphanRemoval=true를 함께 사용하면 부모 엔티티를 통해 자식 엔티티의 생명주기를 관리할 수 있다.
- 스스로 생명주기를 관리하는 엔티티는 em.persist()로 영속화하고, em.remove()로 제거한다.
- 두 옵션을 모두 활성화하면 부모 엔티티를 통해 자식 엔티티의 생명주기를 관리할 수 있다.
- 도메인 주도 설계(DDD)의 Aggregate Root 개념을 구현할 때 유용하다.
'BackEnd > JPA' 카테고리의 다른 글
[JPA] 값 타입(2) (0) | 2024.07.18 |
---|---|
[JPA] 값 타입(1) (0) | 2024.07.17 |
[JPA] 프록시와 연관관계 관리 (0) | 2024.07.16 |
[JPA] 고급 매핑 (0) | 2024.07.15 |
[JPA] 다양한 연관관계 매핑 (0) | 2024.07.14 |