on
[Spring] JPA Auditing으로 생성시간, 수정시간 자동화하기
[Spring] JPA Auditing으로 생성시간, 수정시간 자동화하기
이동욱 님의 스프링 부트와 AWS로 혼자 구현하는 웹 서비스책을 공부하며 정리한 내용입니다. 틀린 정보가 있을 수 있으니 주의하시고 댓글로 남겨주시길 바랍니다.
생성시간/수정시간
보통 엔티티(Entity)에는 해당 데이터의 생성시간과 수정시간을 포함합니다. 언제 만들어졌고 수정되었는지 등에 대한 정보는 차후 유지보수에 있어 굉장히 중요한 정보이기 때문입니다. 하지만 이러한 정보를 계속에서 등록/수정하려면, 코드가 아래처럼 지저분해 지게 됩니다.
public void savePosts() { ... posts.setCreateDate(new LocalDate()); postsRepository.save(posts); }
이런 단순 코드가 모든 테이블, 서비스 코드에 포함된다면 코드가 상당히 지저분해집니다. 이러한 점을 해결하고자 JPA Auditing을 사용합니다!
LocalDate 사용
domain 패키지에 BaseTimeEntity 클래스를 생성합니다.
BaseTimeEntity
package com.hwanld.book.springboot.domain.post; import lombok.Getter; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.EntityListeners; import javax.persistence.MappedSuperclass; import java.time.LocalDateTime; @Getter @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public class DomainTimeEntity { @CreatedDate private LocalDateTime createdDate; @LastModifiedDate private LocalDateTime modifiedDate; }
BaseTimeEntity클래스는 모든 Entity의 상위 클래스가 되어 Entity들의 createdDate, modifiedDate를 자동으로 관리하는 역할을 하게 됩니다.
@MappedSuperclass : JPA Entity 클래스들이 BaseTimeEntity를 상속할 경우 필드들(createdDate, modifiedDate)도 칼럼으로 인식하도록 합니다.
@EntityListener(AuditingEntityListener.class) : BaseTimeEntity 클래스에 Auditing 기능을 포함시킵니다.
@CreatedDate : Entity가 생성되어 저장될 대 시간이 자동 저장됩니다.
@lastModifiedDate : 조회된 Entity의 값을 변경시킬 때 시간이 자동 저장됩니다.
이어서, Posts 클래스가 BaseTimeEntity를 상속받도록 변경합니다.
public class Posts extends BaseTimeEntity { ... }
이어서 JPA Auditing 에노테이션을 모두 활성화 할 수 있도록 Application 클래스에 활성화 에노테이션을 하나 추가합니다.
package com.hwanld.book.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @EnableJpaAuditing //새롭게 추가된 에노테이션 @SpringBootApplication public class Application { public static void main(String [] args) { SpringApplication.run(Application.class, args); } }
테스트 코드 작성하기
PostsRepositoryTest 클래스에 메소드를 추가해서 확인합니다.
package com.hwanld.book.springboot.domain.post; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.time.LocalDateTime; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest public class PostsRepositoryTest { @Autowired PostsRepository postsRepository; @After public void cleanup() { postsRepository.deleteAll(); } @Test public void 게시글저장_불러오기() { //given String title = "테스트 게시글"; String content = "테스트 본문"; postsRepository.save(Posts.builder() .title(title) .content(content) .author("[email protected]") .build()); //when List postsList = postsRepository.findAll(); //then Posts posts = postsList.get(0); assertThat(posts.getTitle()).isEqualTo(title); assertThat(posts.getContent()).isEqualTo(content); } @Test public void BaseTimeEntity_등록() { //given LocalDateTime now = LocalDateTime.of(2019,6,4,0,0,0); postsRepository.save(Posts.builder() .title("title") .content("content") .author("author") .build()); //when List postsList = postsRepository.findAll(); //then Posts posts = postsList.get(0); System.out.println(">>>>>>> createDate=" + posts.getCreatedDate() + ", modifiedDaste = "+posts.getModifiedDate()); assertThat(posts.getCreatedDate()).isAfter(now); assertThat(posts.getModifiedDate()).isAfter(now); } }
from http://hwanld.tistory.com/9 by ccl(A) rewrite - 2021-12-31 01:27:32