Entity를 수정할 때 데이터만 변경하면 된다.
Entity의 변경사항을 데이터베이스에 자동으로 반영하는 기능을 Dirty Checking
Dirty Checking은 영속성 컨텍스트가 관리하는 영속 상태의 Entity에만 적용됨
Entity의 모든 필드를 업데이트한다.
🤔 어떻게 Dirty Checking이 이루어질까?
JPA는 Entity를 영속성 컨텍스트에 보관할 때, 최초의 상태를 복사해서 저장해둔다.
이를 '스냅샷'이라고 부른다.
플러시 시점에 스냅샷과 Entity를 비교해서 변경된 Entity를 찾는다.
'플러시(flush)'는 영속성 컨텍스트의 변경 내용을 데이터베이스에 반영하는 것
✨ Dirty Checking 적용 예시
public void update(String title, String content, String author, int view){
this.title=title;
this.content=content;
this.author=author;
this.view=view;
}
Entity에서 update 메소드
public void updateView(Long id) {
Post post=postRepository.findById(id).get();
post.update(post.getTitle(), post.getContent(), post.getAuthor(),post.getView()+1);
//postRepository.save(post);
}
Dirty Checking
참고 👉 자바 ORM 표준 JPA 프로그래밍
반응형
'ORM > JPA' 카테고리의 다른 글
[Error] org.hibernate.LazyInitializationException: could not initialize proxy (0) | 2022.07.20 |
---|---|
@JoinColumn (0) | 2022.05.19 |
JPA를 이용한 데이터베이스 초기화 (0) | 2022.03.31 |
[Error] No default constructor for entity (0) | 2022.01.25 |
[Error] java.lang.IllegalStateException (1) | 2021.07.24 |