@DynamicInsert
public class Comment{
}
@Test
@Transactional
void commentTest(){
Comment comment = new Comment();
comment.setComment("우와우");
commentRepository.saveAndFlush(comment);
System.out.println("entity cache : " + comment);
entityManager.clear();
System.out.println("from db : " + commentRepository.findById(4L).get());
}
/*
entity cache : Comment(super=BaseEntity(createdAt=2022-03-14T16:57:37.643672,
updatedAt=2022-03-14T16:57:37.643672), id=4, comment=우와우, commentedAt=null)
from db : Comment(super=BaseEntity(createdAt=2022-03-14T16:57:37.643672,
updatedAt=2022-03-14T16:57:37.643672), id=4, comment=우와우,
commentedAt=2022-03-14T16:57:37.720460)
*/
- 기본적으로 entity에 대한 insert와 update는 모든 필드에 대해서 다 적용됨
- null값이 아닌 값에 대해서만 insert 하고 싶을 때, @DynamicInsert
- 만약 변경된 사항에 대해서만 update하고 싶으면 @DynamicUpdate를 적용해야 함 [ Baeldung @Dynamic Update ]
- 변경된 컬럼을 찾기 위해 Hiberante가 직접 Entity의 상태를 추적해야 함. 그래서, Entity의 필드를 변경했을 때 비교해서 sql 쿼리 생성 ⇒ 오버헤드가 약간 발생 (꼭 필요할때만 써라)