====== Hibernate Dirty Checking ====== * [[https://vladmihalcea.com/the-anatomy-of-hibernate-dirty-checking/|The anatomy of Hibernate dirty checking mechanism - Vlad Mihalcea]] * [[https://vladmihalcea.com/how-to-customize-hibernate-dirty-checking-mechanism/|How to customize Hibernate dirty checking mechanism - Vlad Mihalcea]] * [[https://vladmihalcea.com/how-to-enable-bytecode-enhancement-dirty-checking-in-hibernate/|How to enable bytecode enhancement dirty checking in Hibernate - Vlad Mihalcea]] ===== 주의! : 읽기만 했는데 dirty checking 에 의해 update 발생 ===== * Hibernate 는 Entity 조회시에 조회한 Entity 의 복제본을 저장하고 이후에 ''equals''로 각 필드를 비교해서 dirty checking 을 한다. * 따라서 **custom value object를 필드로 사용하는 경우 특히 [[java:jpa:converter|JPA Converter]] 사용시에 equals/hashCode 를 올바로 구현하지 않으면 dirty checking 시 복제된 객체의 reference 주소와 entity 필드의 기존 reference 주소를 비교하면서 dirty 상태로 표시되고 이로 인해 update 가 발생할 수 있다.** * ''@Embeddable''의 경우에는 class 가 아닌 다시 그 각 필드에 대해 equals 를 수행한다. 따라서 ''@Embeddable'' 클래스의 각 필드를 구성하는 클래스에 ''equals/hashCode''가 구현돼 있어야 한다. * 애초에 Java 에서 불변 객체가 확실한 ''LocalDate'', ''LocalDateTime'' 등은 복사하지 않는다고 함. ===== 참조 ===== * [[https://jojoldu.tistory.com/536?category=637935|JPA Entity Select에서 Update 쿼리 발생할 경우]] * [[https://velog.io/@blackbean99/SpringBoot-Select%ED%96%88%EB%8A%94%EB%8D%B0-Update-%EC%BF%BC%EB%A6%AC%EA%B0%80-%EA%B0%99%EC%9D%B4-%EC%83%9D%EC%84%B1%EB%90%A0-%EA%B2%BD%EC%9A%B0|[JPA] Select했는데 Update 쿼리가 같이 생성될 경우]] * [[https://stackoverflow.com/questions/5268466/how-does-hibernate-detect-dirty-state-of-an-entity-object/5268617#5268617|java - How does Hibernate detect dirty state of an entity object? - Stack Overflow]]