728x90
(์ ๊ถ๊ธ์ฆ ํ ์ฉ๋๋ก ์ ๋ฆฌํ๊ฑฐ๋ผ ๋ง ๊น๋ํ๊ฒ ์ ๋ฆฌํ์ง๋ ์์์ต๋๋ค.)
๐ง ManyToOne, OneToOne ์ฐ๊ด๊ด๊ณ
์ผ๋ฐ์ ์ผ๋ก merge ์ merge๋๋ ํน์ ์ํฐํฐ์ ๋ํด์๋ง ์กฐํํฉ๋๋ค.
๊ทธ๋ฌ๋ cascade ์์ฑ์ด MERGE๋ผ๋ฉด merge ์ ์ฐ๊ด๋ ์ํฐํฐ๋ค๋ left join์ผ๋ก ์กฐํํด์ต๋๋ค.
merge()๊ฐ ๋์ด ๊ฐ์ด ๋ณ๊ฒฝ๋๋ ๊ฒ๋ ์ผ๋ฐ์ ์ผ๋ก๋ fk ๊ฐ์ ๋ณ๊ฒฝ๋ง์ด ๋ฐ์๋์ง๋ง,
cascade ์์ฑ์ด merge๋ผ๋ฉด ์ฐ๊ด๋ ์ํฐํฐ์ ๋ณ๊ฒฝ๋ ํจ๊ป ๋ฐ์๋ฉ๋๋ค.
(DB ๊ด์ ์์ ์๊ฐํ๋ฉด ํธํฉ๋๋ค)
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Member(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
}
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Writer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
public Writer(String name, Member member) {
this.name = name;
this.member = member;
}
public void setMember(Member member) {
this.member = member;
}
}
๐ ์ผ๋ฐ merge ์ ๋ฐ์ํ๋ ์กฐํ ์ฟผ๋ฆฌ
Writer๋ง ์กฐํํ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
๐ cascade ์ต์ ์ด MERGE์ผ ๋, merge ์ ๋ฐ์ํ๋ ์กฐํ ์ฟผ๋ฆฌ
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Writer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) // ์ถ๊ฐ
@JoinColumn(name = "member_id")
private Member member;
public Writer(String name, Member member) {
this.name = name;
this.member = member;
}
public void setMember(Member member) {
this.member = member;
}
}
left fetch join์ผ๋ก ์ฐ๊ด๋ Member๊น์ง ๊ฐ์ง๊ณ ์ค๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
๐ณ ๋์ ๊ฐ์ฒด๊ฐ null์ด ๋๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์ฐ๊ด๊ด๊ณ๊ฐ ์ ๊ฑฐ๋ฉ๋๋ค.
@Test
void ๋์_๊ฐ์ฒด๊ฐ_Null_์ด_๋๋_๊ฒฝ์ฐ_์ฐ๊ด๊ด๊ณ๊ฐ_๋์ด์ง๋ค() {
// given
Member mallang = memberRepository.save(new Member("mallang"));
Writer writer = writerRepository.save(new Writer("๋ง๋", mallang));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setMember(null);
writerRepository.save(writer); // merge
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.member()).isNull();
}
๐ณ ๋์ ๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด๋ก ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์ฐ๊ด ๊ฐ์ฒด๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค.
@Test
void ๋์_๊ฐ์ฒด๊ฐ_๋ค๋ฅธ_๊ฐ์ฒด๋ก_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_์ฐ๊ด๊ด๊ณ๋_๋ณ๊ฒฝ๋๋ค() {
// given
Member mallang = memberRepository.save(new Member("mallang"));
Member other = memberRepository.save(new Member("other"));
Writer writer = writerRepository.save(new Writer("๋ง๋", mallang));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setMember(other);
writerRepository.save(writer); // merge
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.member().id()).isEqualTo(other.id());
assertThat(byId.member().name()).isEqualTo("other");
}
๐ณ id๊ฐ ์๋ ๊ฐ์ฒด๋ก ๋ณ๊ฒฝ๋๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ฐธ๊ณ : cascade ์์ฑ์ด MERGE์ธ ๊ฒฝ์ฐ ๋์ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๊ณ ์ฐ๊ด๊ด๊ณ๋ฅผ ์ค์ ํฉ๋๋ค.
@Test
void ๋์_๊ฐ์ฒด์_id๊ฐ_์๋_๊ฒฝ์ฐ_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
// given
Member mallang = memberRepository.save(new Member("mallang"));
Writer writer = writerRepository.save(new Writer("๋ง๋", mallang));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setMember(new Member("other"));
assertThatThrownBy(() -> {
writerRepository.save(writer);
em.flush();
}).isInstanceOf(IllegalStateException.class);
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.member().name()).isEqualTo("mallang");
}
cascade = MERGE์ธ ๊ฒฝ์ฐ
@Test
void ๋์_๊ฐ์ฒด์_id๊ฐ_์๋_๊ฒฝ์ฐ_cascade_์ต์
์ด_MERGE_๋ผ๋ฉด_์ ์ฅ๋๊ณ _์ฐ๊ด๊ด๊ณ๊ฐ_๋ณ๊ฒฝ๋๋ค() {
// given
Member mallang = memberRepository.save(new Member("mallang"));
Writer writer = writerRepository.save(new Writer("๋ง๋", mallang));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setMember(new Member("other"));
writerRepository.save(writer); // merge
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.member().id()).isNotEqualTo(mallang.id());
assertThat(byId.member().name()).isEqualTo("other");
}
๐ณ ๋์ ๊ฐ์ฒด์ ์ผ๋ถ ํ๋๊ฐ ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์ผ๋ฐ์ ์ผ๋ก๋ ๋ฐ์๋์ง ์์ต๋๋ค.
์ฐธ๊ณ : cascade ์์ฑ์ด MERGE์ธ ๊ฒฝ์ฐ ๋ฐ์๋ฉ๋๋ค.
@Test
void ๋์_๊ฐ์ฒด์_์ผ๋ถ_ํ๋๊ฐ_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_๋ฐ์๋์ง_์๋๋ค() {
// given
Member mallang = memberRepository.save(new Member("mallang"));
Writer writer = writerRepository.save(new Writer("๋ง๋", mallang));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
Member member = writer.member();
member.setName("๋ณ๊ฒฝ๋ง๋");
System.out.println("๋ณ๊ฒฝํ๋ ค๋ ์ด๋ฆ: " + writer.member().name());
writerRepository.save(writer); // merge
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.member().name()).isEqualTo("mallang");
}
cascade = MERGE์ธ ๊ฒฝ์ฐ
@Test
void ๋์_๊ฐ์ฒด์_์ผ๋ถ_ํ๋๊ฐ_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_cascade_์ต์
์ด_MERGE_๋ผ๋ฉด_๋ฐ์๋๋ค() {
// given
Member mallang = memberRepository.save(new Member("mallang"));
Writer writer = writerRepository.save(new Writer("๋ง๋", mallang));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
Member member = writer.member();
member.setName("๋ณ๊ฒฝ๋ง๋");
System.out.println("๋ณ๊ฒฝํ๋ ค๋ ์ด๋ฆ: " + writer.member().name());
writerRepository.save(writer); // merge
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.member().name()).isEqualTo("๋ณ๊ฒฝ๋ง๋");
}
๐ง OneToMany ์ฐ๊ด๊ด๊ณ (One์ด ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ด ์๋ ๊ฒฝ์ฐ)
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Writer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "writer")
private List<Post> posts = new ArrayList<>();
public Writer(String name) {
this.name = name;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
public void addPost(Post post) {
post.setWriter(this);
posts.add(post);
}
}
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "writer_id")
private Writer writer;
public Post(String title) {
this.title = title;
}
public void setTitle(String title) {
this.title = title;
}
public void setWriter(Writer writer) {
this.writer = writer;
}
}
๐ ์ผ๋ฐ merge ์ ๋ฐ์ํ๋ ์กฐํ ์ฟผ๋ฆฌ
@Test
void ์ผ๋ฐ_merge() {
// given
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
Writer writer = new Writer("mallang");
writer.addPost(post1);
writer.addPost(post2);
Writer saved = writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writerRepository.save(saved);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
}
@Test
void ์ผ๋ฐ_merge2() {
// given
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
Writer writer = new Writer("mallang");
writer.addPost(post1);
writer.addPost(post2);
Writer saved = writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
Writer find = writerRepository.findById(saved.id()).get();
em.flush();
em.clear();
System.out.println("\n========[Find]========\n");
writerRepository.save(find);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
}
๐ cascade ์ต์ ์ด MERGE์ผ ๋, merge ์ ๋ฐ์ํ๋ ์กฐํ ์ฟผ๋ฆฌ
@Test
void ์ผ๋ฐ_merge() {
// given
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
Writer writer = new Writer("mallang");
writer.addPost(post1);
writer.addPost(post2);
Writer saved = writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writerRepository.save(saved);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
}
@Test
void ์ผ๋ฐ_merge2() {
// given
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
Writer writer = new Writer("mallang");
writer.addPost(post1);
writer.addPost(post2);
Writer saved = writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
Writer find = writerRepository.findById(saved.id()).get();
em.flush();
em.clear();
System.out.println("\n========[Find]========\n");
writerRepository.save(find);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
}
๐ณ List๊ฐ null๋ก ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์๋ฌด๊ฒ๋ ๋ฌ๋ผ์ง์ง ์์ต๋๋ค.
@Test
void List_๊ฐ_null๋ก_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_์๋ฌด๋ฐ_๋ณ๊ฒฝ์ด_์๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
writer.addPost(post1);
writer.addPost(post2);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setPosts(null);
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
}
๐ณ ๋น์ด์๋ List๋ก ๋ณ๊ฒฝ๋๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์๋ฌด๊ฒ๋ ๋ฌ๋ผ์ง์ง ์์ต๋๋ค.
@Test
void ๋น์ด์๋_List_๋ก_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_์๋ฌด๋ฐ_๋ณ๊ฒฝ์ด_์๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
writer.addPost(post1);
writer.addPost(post2);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setPosts(Collections.emptyList());
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
}
๐ณ List๋ฅผ ๋น์ฐ๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์๋ฌด๊ฒ๋ ๋ฌ๋ผ์ง์ง ์์ต๋๋ค.
cascade ์ต์ ์ด MERGE์ด๋ฉฐ, List๋ฅผ ๋น์ธ ๋ ์ฐ๊ด๊ด๊ณ ์ฐธ์กฐ๋ฅผ ์ ๊ฑฐํ๋๋ผ๋ ์๋ฌด๊ฒ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
@Test
void List_๋ฅผ_๋น์ฐ๋_๊ฒฝ์ฐ_์๋ฌด๋ฐ_๋ณ๊ฒฝ์ด_์๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
writer.addPost(post1);
writer.addPost(post2);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
List<Post> posts = writer.posts();
posts.clear();
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
}
@Test
void List_๋ฅผ_๋น์ฐ๋_๊ฒฝ์ฐ_cascade_MERGE_์_์ฐ๊ด๊ด๊ณ_์ฐธ์กฐ๋ฅผ_์ ๊ฑฐํ๋๋ผ๋_์๋ฌด๋ฐ_๋ณ๊ฒฝ์ด_์๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
writer.addPost(post1);
writer.addPost(post2);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
List<Post> posts = writer.posts();
for (Post post : posts) {
post.setWriter(null);
}
posts.clear();
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
}
๐ณ List์ ์ํ ์ํฐํฐ ์ค ํน์ ์ํฐํฐ๊ฐ ๋ฌ๋ผ์ง ๊ฒฝ์ฐ(id ๋ณ๊ฒฝ, ์ถ๊ฐ, ์ ๊ฑฐ ๋ฑ)
๊ฒฐ๋ก : ์๋ฌด๊ฒ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
@Test
void List์_์ํ_์ํฐํฐ_์ค_ํน์ _์ํฐํฐ๊ฐ_๋ฌ๋ผ์ง_๊ฒฝ์ฐ_์๋ฌด๋ฐ_๋ณ๊ฒฝ์ด_์๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
writer.addPost(post1);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.addPost(post2);
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(1);
}
๐ณ List์ ์ํ ์ํฐํฐ ์ค ํน์ ์ํฐํฐ์ ์ผ๋ถ ํ๋(id ์ ์ธ)๊ฐ ๋ฌ๋ผ์ง ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์๋ฌด๊ฒ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
@Test
void List์_์ํ_์ํฐํฐ_์ค_ํน์ _์ํฐํฐ์_์ผ๋ถ_ํ๋๊ฐ_๋ฌ๋ผ์ง_๊ฒฝ์ฐ_์๋ฌด๋ฐ_๋ณ๊ฒฝ์ด_์๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
Post post1 = postRepository.save(new Post("์ ๋ชฉ1"));
Post post2 = postRepository.save(new Post("์ ๋ชฉ2"));
writer.addPost(post1);
writer.addPost(post2);
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
List<Post> posts = writer.posts();
for (Post post : posts) {
post.setTitle("dd");
}
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
assertThat(byId.posts().get(0).title()).isEqualTo("์ ๋ชฉ1");
}
๐ง OneToMany ์ฐ๊ด๊ด๊ณ (One์ด ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ธ ๊ฒฝ์ฐ)
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Writer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany
@JoinColumn(name = "writer_id")
private List<Post> posts = new ArrayList<>();
public Writer(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void setPosts(List<Post> posts) {
this.posts = posts;
}
public void addPost(Post post) {
posts.add(post);
}
}
@NoArgsConstructor
@Accessors(fluent = true)
@Getter
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
public Post(String title) {
this.title = title;
}
public void setTitle(String title) {
this.title = title;
}
}
๐ณ List๊ฐ null๋ก ๋ณ๊ฒฝ๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์ฐ๊ด๊ด๊ณ๊ฐ ์ ๊ฑฐ๋ฉ๋๋ค.
@Test
void List_๊ฐ_null๋ก_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_์ฐ๊ด๊ด๊ณ๊ฐ_์ ๊ฑฐ๋๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
writer.addPost(new Post("์ ๋ชฉ1"));
writer.addPost(new Post("์ ๋ชฉ2"));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setPosts(null);
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(0);
}
๐ณ ๋น์ด์๋ List๋ก ๋ณ๊ฒฝ๋๋ ๊ฒฝ์ฐ
๊ฒฐ๋ก : ์ฐ๊ด๊ด๊ณ๊ฐ ์ ๊ฑฐ๋ฉ๋๋ค.
@Test
void List_๊ฐ_๋น์ด์๋_๋ฆฌ์คํธ๋ก_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ_์ฐ๊ด๊ด๊ณ๊ฐ_์ ๊ฑฐ๋๋ค() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
writer.addPost(new Post("์ ๋ชฉ1"));
writer.addPost(new Post("์ ๋ชฉ2"));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
writer.setPosts(Collections.emptyList());
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(0);
}
๐ณ List์ ์ํ ์ํฐํฐ ์ค ํน์ ์ํฐํฐ๊ฐ ๋ฌ๋ผ์ง ๊ฒฝ์ฐ
๊ฒฐ๋ก : cascade, orphanRemoval ๋ฑ์ ์ต์ ์ ๋ฐ๋ผ ์ฌ๋ฌ๊ฐ์ง ์ํฉ์ด ๋ฐ์ํฉ๋๋ค.
(์ง์ ํ์ธํ์๊ธธ..)
๐ณ List์ ์ํ ์ํฐํฐ ์ค ํน์ ์ํฐํฐ์ ์ผ๋ถ ํ๋(id ์ ์ธ)๊ฐ ๋ฌ๋ผ์ง ๊ฒฝ์ฐ
๊ฒฐ๋ก : cascade๊ฐ MERGE ์ ๋ณ๊ฒฝ๋๊ณ , ๊ทธ๋ ์ง ์๋ค๋ฉด ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค.
cascade๊ฐ MERGE๊ฐ ์๋ ๊ฒฝ์ฐ
@Test
void List์_์ํ_์ํฐํฐ_์ค_ํน์ _์ํฐํฐ์_ํ๋๊ฐ_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
writer.addPost(new Post("์ ๋ชฉ1"));
writer.addPost(new Post("์ ๋ชฉ2"));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
for (Post post : writer.posts()) {
post.setTitle("dd");
}
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
assertThat(byId.posts().get(1).title()).isEqualTo("์ ๋ชฉ2");
}
cascade๊ฐ MERGE์ธ ๊ฒฝ์ฐ
@Test
void List์_์ํ_์ํฐํฐ_์ค_ํน์ _์ํฐํฐ์_ํ๋๊ฐ_๋ณ๊ฒฝ๋_๊ฒฝ์ฐ() {
// given
Writer writer = writerRepository.save(new Writer("mallang"));
writer.addPost(new Post("์ ๋ชฉ1"));
writer.addPost(new Post("์ ๋ชฉ2"));
em.flush();
em.clear();
System.out.println("\n========[๋ฐ์ดํฐ ์ ์ฅ]========\n");
// when
for (Post post : writer.posts()) {
post.setTitle("dd");
}
writerRepository.save(writer);
em.flush();
em.clear();
System.out.println("\n========[Merge]========\n");
// then
Writer byId = writerRepository.getById(writer.id());
assertThat(byId.posts()).hasSize(2);
assertThat(byId.posts().get(1).title()).isEqualTo("dd");
}
728x90