참고
기존 Date와 Calendar 클래스의 문제점
- 불변(변경이 불가능한) 객체가 아님
- 멀티스레드 환경에서 언제든 문제가 발생할 수 있음
- Calendar의 Month 값 설계 오류
- 10월 : Calendar.OCTOBER의 값이 '9'로 표기
@Getter
@MappedSuperclass // JPA Entity 클래스들이 BaseTimeEntity를 상속할 경우 필드들(createDate, modifiedDate)도 칼럼으로 인식하도록 합니다.
@EntityListeners(AuditingEntityListener.class) // BaseTimeEntity 클래스에 Auditing 기능을 포함시킵니다.
public abstract class BaseTimeEntity {
@CreatedDate // 생성 시간
private LocalDateTime createdDate;
@LastModifiedDate // 수정 시간
private LocalDateTime modifiedDate;
}
public class Posts extends BaseTimeEntity {
//...
@EnableJpaAuditing // JPA Auditing 활성화
@SpringBootApplication
public class AwsJpaApplication {
public static void main(String[] args) {
SpringApplication.run(AwsJpaApplication.class, args);
}
}
@Test
public void baseTimeEntity() {
LocalDateTime dateTime = LocalDateTime.of(2022, 7, 29, 0, 0);
postsRepository.save(Posts.builder().title("title").content("content").author("author").build());
List<Posts> postsList = postsRepository.findAll();
Posts posts = postsList.get(0);
System.out.println("1. ==> createDate : " + posts.getCreatedDate());
System.out.println("2. ==> modifiedDate : " + posts.getModifiedDate());
assertThat(posts.getCreatedDate()).isAfter(dateTime);
assertThat(posts.getModifiedDate()).isAfter(dateTime);
}
Hibernate:
insert
into
posts
(created_date, modified_date, author, content, title)
values
(?, ?, ?, ?, ?)
Hibernate:
select
posts0_.id as id1_0_,
posts0_.created_date as created_2_0_,
posts0_.modified_date as modified3_0_,
posts0_.author as author4_0_,
posts0_.content as content5_0_,
posts0_.title as title6_0_
from
posts posts0_
1. ==> createDate : 2022-07-29T16:24:23.696123
2. ==> modifiedDate : 2022-07-29T16:24:23.696123
Hibernate:
select
posts0_.id as id1_0_,
posts0_.created_date as created_2_0_,
posts0_.modified_date as modified3_0_,
posts0_.author as author4_0_,
posts0_.content as content5_0_,
posts0_.title as title6_0_
from
posts posts0_
Hibernate:
delete
from
posts
where
id=?