2023-07-25-TIL

Today I Learned

Magic Number Checking

  • https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/filefilter/MagicNumberFileFilter.html
  • https://stackoverflow.com/questions/41206540/how-to-get-the-magic-number-from-file-in-java

@Version and @Audited

JPA에서 @Version@Audited는 기본적으로 dirty checking에서 isNew가 true일 때 동작한다. 즉, @Version이나 @Audited가 붙어있는 필드는 setter로 임의로 변경하지 않는 이상 변경으로 감지하지 못한다는 것이다. 따라서 엔티티 내용의 변경이 없더라도 사용자로부터 ‘저장' 요청을 받았을 때 무조건 버전업 및 수정일시를 남기고 싶다면 setter를 통해서 강제로 트리거해주어야 한다.

@PreUpdate는 dirty checking이 유효할 떄 update 쿼리가 날아가고 그 때 쿼리 실행 직전에 동작하므로 결국 setter를 통한 강제 버전업 밖에는 방법이 없다.

  • https://stackoverflow.com/questions/13374604/when-to-use-version-and-audited-in-hibernate
  • https://stackoverflow.com/questions/51709727/spring-boot-jpacreateddate-lastmodifieddate-not-being-populated-when-saving-th
  • https://docs.spring.io/spring-data/jpa/docs/1.7.0.DATAJPA-580-SNAPSHOT/reference/html/auditing.html
  • https://webcoding-start.tistory.com/53
  • https://newwisdom.tistory.com/109
  • https://velog.io/@yebali/Spring-Data-JPA-Auditing
  • https://devhan.tistory.com/209
  • https://www.linkedin.com/pulse/history-audit-javajpahibernate-entity-classes-shubham-ambatkar
  • https://docs.spring.io/spring-data/jpa/docs/1.7.0.DATAJPA-580-SNAPSHOT/reference/html/auditing.html

jpa history table insert only updated

‘저장' 요청을 받았지만 실제 변경한 내용이 없다면 히스토리도 남기지 않도록 할수는 없는가? @PreUpdate에 설정할 수 있다면 좋겠지만, @PreUpdate는 엔티티에 선언되어야하고, 엔티티에서 다른 히스토리 레파지토리를 주입받고 쿼리를 실행하는 것은 매우 권장되지 않는다.

Enver같은 도구를 사용한다면 깔끔하게 해결할 수 있을지도 모른다.

  • https://stackoverflow.com/questions/37927755/spring-jpa-hibernate-database-historical-changes-at-column-level
  • https://medium.com/programmingmitra-com/spring-data-jpa-auditing-saving-createdby-createddate-lastmodifiedby-lastmodifieddate-c2d64c42998e
  • https://sunitc.dev/2020/01/21/spring-boot-how-to-add-jpa-hibernate-envers-auditing/
  • https://techwithmaddy.com/how-to-update-or-insert-a-record-in-spring-data-jpa
  • https://stackoverflow.com/questions/45472635/update-history-table-with-old-and-new-values-using-spring-boot-jpa
  • https://escapefromcoding.tistory.com/550
  • https://www.geeksforgeeks.org/spring-data-jpa-insert-data-in-mysql-table/
  • https://github.com/spring-projects/spring-data-jpa/issues/1905
  • https://www.javacodegeeks.com/2013/09/how-to-maintain-history-of-tables-in-hibernate.html
  • https://stackoverflow.com/questions/59002044/how-to-keep-a-history-of-edit-of-entities-in-a-jpa-application
  • https://levelup.gitconnected.com/manage-a-history-of-domain-model-changes-with-spring-609d2613fc7d

Enver

  • https://hibernate.org/orm/envers/
  • https://vladmihalcea.com/the-best-way-to-implement-an-audit-log-using-hibernate-envers/
  • https://discourse.hibernate.org/t/envers-what-is-the-recommended-way-to-save-the-current-state-of-all-entities-in-an-existing-database/7195
  • https://docs.jboss.org/envers/docs/

Version Up by @LastModifiedDate

  • https://stackoverflow.com/questions/50194996/lastmodifieddate-causes-additional-update-in-database
  • https://github.com/spring-projects/spring-data-jpa/issues/1481

CDC(Change Data Capture)

  • https://vladmihalcea.com/a-beginners-guide-to-cdc-change-data-capture/

Flush vs Persist

persist는 말 그대로 영속성 계층에 올려두는 것이고, flush는 영속성 계층에 있는 쿼리들을 DB로 날리는 것이다.

  • https://medium.com/javarevisited/spring-data-jpa-persist-vs-flush-8b972285b71d

SQL Performance Best Practices

  • https://www.cockroachlabs.com/docs/stable/performance-best-practices-overview

Insert or Update

  • https://damuliu.medium.com/spring-data-jpa-insert-or-update-50ae74833388