고아 객체 생성 조건
666
작성한 질문수 43
안녕하세요 영한님!
질문드립니다.
고아 객체는 부모 엔티티와 연관관계가 끊어진 자식 엔티티 라고 이해했습니다.
고아 객체가 생성되는 조건은
부모 엔티티 삭제
부모 엔티티가 삭제 되면 자식 엔티티를 고아객체로 판단 합니다.e.g)
em.remove(parent);부모 엔티티에 있는 자식 엔티티 컬렉션 제거
연관관계가 끊어진 자식 객체를 고아객체로 판단 합니다.e.g)
parent.getChild().remove(0);
결과적으로 orphanRemoval = true를 설정하면
자식 엔티티(고아 객체)는 부모 엔티티와 함께 삭제 되거나
자식 엔티티(고아 객체)만 삭제 된다.
맞게 이해하고 있는 것 일까요?
감사합니다.^^
답변 1
1
안녕하세요. 개발하는쿼카님^^
제가 직접 정답을 알려드릴 수 도 있지만, 그러면 더 많은 것을 얻어가지 못합니다.
개발자는 궁금한 부분을 직접 코드로 테스트 해볼 때 가장 많이 배울 수 있습니다.
해당 부분을 코드로 직접 테스트해보시고, 그 결과를 공유해주세요. 그러면 함께 공부하는 분들께도 큰 도움이 될거에요.
그럼 테스트 해보시고 결과도 정리해서 공유 부탁드립니다.
감사합니다.
1
답변 감사의 말씀 드립니다. 영한님^^!
제가 테스트 한 내용을 공유 드립니다.
테스트 결과 공유 드립니다.!
테스트 결과 고아객체 생성 조건은 다음과 같이 2가지 입니다.
부모 엔티티 삭제
부모 엔티티가 삭제 되면 자식 엔티티를 고아객체로 판단 합니다.e.g)
em.remove(parent);부모 엔티티에 있는 자식 엔티티 컬렉션 제거
연관관계가 끊어진 자식 객체를 고아객체로 판단 합니다.e.g)
parent.getChild().remove(0);
제가 이해하고 있는 것이 맞았습니다!🤗
orphanRemoval = true를 설정 🙆♂️
File
@Entity
@Getter @Setter
public class File {
@Id
@GeneratedValue
private Long id;
private String name;
private String path;
// 연관관계 주인
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "file_id")
private Post post;
}Post
@Entity
@Getter @Setter
public class Post {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "post", orphanRemoval = true)
private List<File> files = new ArrayList<>();
public void changePost(File file) {
files.add(file);
file.setPost(this);
}
}실험 <부모 엔티티 삭제(em.remove(parent);)>
tx.begin();
File file1 = new File();
file1.setName("둘리");
file1.setPath("/image/둘리.png");
File file2 = new File();
file2.setName("도우너");
file2.setPath("/image/도우너.png");
Post post = new Post();
post.setName("아기공룡 둘리");
post.changePost(file1);
post.changePost(file2);
em.persist(post);
em.persist(file1);
em.persist(file2);
em.flush();
em.clear();
// 부모 엔티티 삭제
Post findPost = em.find(Post.class, post.getId());
em.remove(findPost);
tx.commit();결과
Hibernate:
/* insert com.study.purejpa.Post
*/ insert
into
Post
(name, id)
values
(?, ?)
Hibernate:
/* insert com.study.purejpa.File
*/ insert
into
File
(name, path, file_id, id)
values
(?, ?, ?, ?)
Hibernate:
/* insert com.study.purejpa.File
*/ insert
into
File
(name, path, file_id, id)
values
(?, ?, ?, ?)
Hibernate:
select
post0_.id as id1_11_0_,
post0_.name as name2_11_0_
from
Post post0_
where
post0_.id=?
Hibernate:
select
files0_.file_id as file_id4_5_0_,
files0_.id as id1_5_0_,
files0_.id as id1_5_1_,
files0_.name as name2_5_1_,
files0_.path as path3_5_1_,
files0_.file_id as file_id4_5_1_
from
File files0_
where
files0_.file_id=?
Hibernate:
/* delete com.study.purejpa.File */ delete
from
File
where
id=?
Hibernate:
/* delete com.study.purejpa.File */ delete
from
File
where
id=?
Hibernate:
/* delete com.study.purejpa.Post */ delete
from
Post
where
id=?
부모 엔티티를 삭제하면(em.remove(findPost);) 자식 엔티티가 먼저 삭제되고, 부모 엔티티가 삭제되는 것을 확인할 수 있습니다.
마치 CascadeType.REMOVE 처럼 동작 합니다.!!!
실험 <부모 엔티티에 있는 자식 엔티티 컬렉션 제거(parent.getChild().remove(0);)>
tx.begin();
File file1 = new File();
file1.setName("둘리");
file1.setPath("/image/둘리.png");
File file2 = new File();
file2.setName("도우너");
file2.setPath("/image/도우너.png");
Post post = new Post();
post.setName("아기공룡 둘리");
post.changePost(file1);
post.changePost(file2);
em.persist(post);
em.persist(file1);
em.persist(file2);
em.flush();
em.clear();
Post findPost = em.find(Post.class, post.getId());
// 부모와 연관관계 끊기
// 자식 엔티티를 컬렉션에서 제거
findPost.getFiles().remove(0);
tx.commit();
결과
Hibernate:
/* insert com.study.purejpa.Post
*/ insert
into
Post
(name, id)
values
(?, ?)
Hibernate:
/* insert com.study.purejpa.File
*/ insert
into
File
(name, path, file_id, id)
values
(?, ?, ?, ?)
Hibernate:
/* insert com.study.purejpa.File
*/ insert
into
File
(name, path, file_id, id)
values
(?, ?, ?, ?)
Hibernate:
select
post0_.id as id1_11_0_,
post0_.name as name2_11_0_
from
Post post0_
where
post0_.id=?
Hibernate:
select
files0_.file_id as file_id4_5_0_,
files0_.id as id1_5_0_,
files0_.id as id1_5_1_,
files0_.name as name2_5_1_,
files0_.path as path3_5_1_,
files0_.file_id as file_id4_5_1_
from
File files0_
where
files0_.file_id=?
Hibernate:
/* delete com.study.purejpa.File */ delete
from
File
where
id=?부모 엔티티와 자식 엔티티의 연관관계가 끊어지니 자식 엔티티에 대한 delete SQL이 발생 했습니다.!!
이때 orphanRemoval을 적용하기 위해서는 CascadeType.PERSIST를 포함시켜야합니다.
https://www.inflearn.com/questions/137740/orphanremoval%EA%B3%BC-cascade%EC%9D%98-%EA%B4%80%EA%B3%84
orphanRemoval = true를 설정 🙅♂️
File
@Entity
@Getter @Setter
public class File {
@Id
@GeneratedValue
private Long id;
private String name;
private String path;
// 연관관계 주인
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "file_id")
private Post post;
}Post
@Entity
@Getter @Setter
public class Post {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "post")
private List<File> files = new ArrayList<>();
public void changePost(File file) {
files.add(file);
file.setPost(this);
}
}실험
tx.begin();
File file1 = new File();
file1.setName("둘리");
file1.setPath("/image/둘리.png");
File file2 = new File();
file2.setName("도우너");
file2.setPath("/image/도우너.png");
Post post = new Post();
post.setName("아기공룡 둘리");
post.changePost(file1);
post.changePost(file2);
em.persist(post);
em.persist(file1);
em.persist(file2);
em.flush();
em.clear();
Post findPost = em.find(Post.class, post.getId());
// 부모와 연관관계 끊기
findPost.getFiles().remove(0);
tx.commit();결과
Hibernate:
/* insert com.study.purejpa.Post
*/ insert
into
Post
(name, id)
values
(?, ?)
Hibernate:
/* insert com.study.purejpa.File
*/ insert
into
File
(name, path, file_id, id)
values
(?, ?, ?, ?)
Hibernate:
/* insert com.study.purejpa.File
*/ insert
into
File
(name, path, file_id, id)
values
(?, ?, ?, ?)
Hibernate:
select
post0_.id as id1_11_0_,
post0_.name as name2_11_0_
from
Post post0_
where
post0_.id=?
Hibernate:
select
files0_.file_id as file_id4_5_0_,
files0_.id as id1_5_0_,
files0_.id as id1_5_1_,
files0_.name as name2_5_1_,
files0_.path as path3_5_1_,
files0_.file_id as file_id4_5_1_
from
File files0_
where
files0_.file_id=?지연로딩으로 설정했기 때문에 findPost.getFiles().remove(0); 시점에서 select * from File ... SQL이 발생합니다.
그리고 orphanRemoval를 따로 설정하지 않아서 부모 엔티티와 자식 엔티티의 연관관계가 끊어졌음에도 delete SQL이 발생하지 않았습니다.!
감사합니다.^^
벌크연산에서 member.getAge 호출 시 영속성 컨텍스트에서 데이터를 가져오는건가요?
0
28
2
inheritance startegy 선택시 고려사항
0
22
1
Entity 동등성 비교
0
21
1
실무 조언 관련 질문입니다.
0
47
1
H2데이터베이스 파일 생성
0
56
2
서브쿼리 강의에서 ALL 예시 관련 질문드립니다.
0
53
2
수정또는 삭제시 영속성 엔티티에 값이 무조건 있어야 하나요?
0
52
1
JPQL 메소드와 락
0
55
1
Delivery @OneToOne
0
60
1
17강 4~5분대 테이블 값 조회가 안됩니다.
0
94
2
UnsupportedOperationException 발생
0
86
3
H2 Database 연결이 안됩니다.
0
95
2
연관관계 매핑 질문드립니다.
0
85
2
h2데이터베이스 실행오류
0
108
2
persistence.xml
0
108
2
양방향 연관관계에서 연관관계의 주인(mappedBy)을 왜 꼭 정해야 하나요?
0
80
1
영속성 컨텍스트
0
66
1
JPA 프록시
0
96
1
Native Query와 MyBatis
0
70
1
영속성 컨텍스트는 어떤 메모리에 저장되는건가요?
0
87
1
임베디드 타입 예시 코드 관련 질문
0
115
3
명시적 조인에서 별칭을 주면 왜 객체에 접근할 수 있나요
0
95
3
인텔리제이 패키지 커서 단축키 질문
0
108
2
혹시 현재는 ID 데이터 타입이 String이면 안되나요?
0
145
1





