// Person 객체를 merge 할 경우 하위 Phone 객체까지 같이 영속성 Context 로 올라오게 된다. entityManager.merge(person); }
Hibernate: insert into person (id, name) values (null, ?) Hibernate: insert into phone (id, person_id, phone_number) values (null, ?, ?) Hibernate: select person0_.id as id1_0_1_, person0_.name as name2_0_1_, phones1_.person_id as person_i3_1_3_, phones1_.id as id1_1_3_, phones1_.id as id1_1_0_, phones1_.person_id as person_i3_1_0_, phones1_.phone_number as phone_nu2_1_0_ from person person0_ left outer join phone phones1_ on person0_.id=phones1_.person_id where person0_.id=? Hibernate: update phone set person_id=?, phone_number=? where id=? Hibernate: update person set name=? where id=?
person.setName("test2"); phone.setPhoneNumber("010-1111-2222"); // Person 객체와 하위 Phone 객체까지 영속성 Context 내 데이터가 초기화 된다. entityManager.refresh(person);
// DataBase 에 데이터가 반영되지 않은 상태에서 Refresh 작업이 일어났기 때문에 상태가 초기화 된다. assertThat(person.getName()).isEqualTo("tester"); assertThat(phone.getPhoneNumber()).isEqualTo("010-1234-1234"); }
update 가 이뤄지지 않은 것을 확인할 수 있다.
Hibernate: insert into person (id, name) values (null, ?) Hibernate: insert into phone (id, person_id, phone_number) values (null, ?, ?) Hibernate: select phone0_.id as id1_1_0_, phone0_.person_id as person_i3_1_0_, phone0_.phone_number as phone_nu2_1_0_ from phone phone0_ where phone0_.id=? Hibernate: select person0_.id as id1_0_1_, person0_.name as name2_0_1_, phones1_.person_id as person_i3_1_3_, phones1_.id as id1_1_3_, phones1_.id as id1_1_0_, phones1_.person_id as person_i3_1_0_, phones1_.phone_number as phone_nu2_1_0_ from person person0_ left outer join phone phones1_ on person0_.id=phones1_.person_id where person0_.id=?
6. CascadeType.DETACH
상위 Entity 를 영속성 컨텍스트에서 제거할 때 하위 Entity 까지 같이 제거한다.