===> prePersist() method 호출 Hibernate: call next value for hibernate_sequence Hibernate: insert into user (created_at, email, gender, name, updated_at, id) values (?, ?, ?, ?, ?, ?) ===> postPersist() method 호출 Hibernate: select user0_.id as id1_0_0_, user0_.created_at as created_2_0_0_, user0_.email as email3_0_0_, user0_.gender as gender4_0_0_, user0_.name as name5_0_0_, user0_.updated_at as updated_6_0_0_ from user user0_ where user0_.id=? ===> postLoad() method 호출 Hibernate: select user0_.id as id1_0_0_, user0_.created_at as created_2_0_0_, user0_.email as email3_0_0_, user0_.gender as gender4_0_0_, user0_.name as name5_0_0_, user0_.updated_at as updated_6_0_0_ from user user0_ where user0_.id=? ===> postLoad() method 호출 ===> preUpdate() method 호출 Hibernate: update user set created_at=?, email=?, gender=?, name=?, updated_at=? where id=? ===> postUpdate() method 호출 Hibernate: select user0_.id as id1_0_0_, user0_.created_at as created_2_0_0_, user0_.email as email3_0_0_, user0_.gender as gender4_0_0_, user0_.name as name5_0_0_, user0_.updated_at as updated_6_0_0_ from user user0_ where user0_.id=? ===> postLoad() method 호출 ===> preRemove() method 호출 Hibernate: delete from user where id=? ===> postRemove() method 호출
활용
실제로는 데이터베이스 오디팅에 자주 사용된다. 오디팅(Auditing)이란 선택된 DB 사용자의 활동을 감시하는 프로세스이다.
Entity의 필드 값에 대한 prePersist, preUpdate가 자주 사용한다.
INSERT 이전
UPDATE 이전
Entity에 직접 구현
Entity에서 아래와 같이 객체의 생성된 날짜와 수정 날짜를 INSERT 이전, UPDATE 이전에 시행하도록 설정할 수 있다.