-
[Spring Boot] JPA 게시판 만들기(3) - JPA Auditing 설정Spring Boot/2.3.x - JPA 게시판 만들기 2020. 7. 20. 16:08반응형
일반적으로 테이블을 설계할 때 데이터의 생성시간과 수정시간 컬럼을 포함하는데, JPA Auditing을 사용하면 생성시간과 수정시간을 자동으로 관리할 수 있습니다.
1.BaseTimeEntity 구현
com.rest.api.common.domain 패키지를 생성한 후 BaseTimeEntity 클래스르 추가하세요.
BaseTimeEntity.java
더보기123456789101112131415161718192021222324252627package com.rest.api.common.domain;import java.time.LocalDateTime;import javax.persistence.Column;import javax.persistence.EntityListeners;import javax.persistence.MappedSuperclass;import org.springframework.data.annotation.CreatedDate;import org.springframework.data.annotation.LastModifiedDate;import org.springframework.data.jpa.domain.support.AuditingEntityListener;import lombok.Getter;@Getter@MappedSuperclass@EntityListeners(AuditingEntityListener.class)public abstract class BaseTimeEntity {@CreatedDate@Column(name = "created_date")private LocalDateTime createdDate;@LastModifiedDate@Column(name = "modifiedDate")private LocalDateTime modifiedDate;}cs @MappedSuperclass
JPA Entity 클래스들이 BaseTimeEntity을 상속할 경우 필드들도 컬럼으로 인식할 수 있도록 합니다.
@EntityListeners(AuditingEntityListener.class)
BaseTimeEntity 클래스에 Auditing 기능을 포함하도록 합니다.
@CreatedDate
Entity가 생성되어 저장될 때 시간이 자동으로 저장되도록 합니다.
@LastModifiedDate
Entity의 값을 변경할 때 시간이 자동으로 저장되록 합니다.
2.Board 수정
Board 클래스가 BaseTimeEntity를 상속받도록 수정하세요.
Board.java
더보기1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package com.rest.api.board.domain;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Lob;import javax.persistence.Table;import com.rest.api.common.domain.BaseTimeEntity;import lombok.Builder;import lombok.Getter;import lombok.NoArgsConstructor;@Getter@NoArgsConstructor@Entity@Table(name = "tbl_board")public class Board extends BaseTimeEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "board_seq")private Long boardSeq;@Lob@Column(name = "board_subject")private String boardSubject;@Column(name = "board_writer")private String boardWriter;@Lob@Column(name = "board_content")private String boardContent;@Builderpublic Board(String boardSubject, String boardWriter, String boardContent) {this.boardSubject = boardSubject;this.boardWriter = boardWriter;this.boardContent = boardContent;}public void update(String boardSubject, String boardContent) {this.boardSubject = boardSubject;this.boardContent = boardContent;}}cs 3.Application 수정
JPA Auditing 어노테이션들을 활성화하기 위해 Application 클래스에 @EnableJpaAuditing 어노테이션을 추가하세요.
JpaSampleApplication.java
더보기123456789101112131415161718192021package com.rest.api;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;import org.springframework.data.jpa.repository.config.EnableJpaAuditing;@EnableJpaAuditing@SpringBootApplicationpublic class JpaSampleApplication extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(JpaSampleApplication.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(JpaSampleApplication.class);}}cs 4.JPA Auditing 테스트 코드 구현
com.rest.api.board.repository 패키지를 생성한 후 BoardRepositoryTest 클래스를 추가하세요.
BoardRepositoryTest.java
더보기12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849package com.rest.api.board.repository;import static org.assertj.core.api.Assertions.assertThat;import java.time.LocalDateTime;import java.util.List;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 com.rest.api.board.domain.Board;@RunWith(SpringRunner.class)@SpringBootTestpublic class BoardRepositoryTest {@AutowiredBoardRepository boardRepository;@Testpublic void baseTimeEntity() {String boardSubject = "게시글_제목_저장_01";String boardWriter = "게시글_작성자_저장_01";String boardContent = "게시글_내용_저장_01";LocalDateTime now = LocalDateTime.of(2020, 7, 20, 0, 0, 0);boardRepository.save(Board.builder().boardSubject(boardSubject).boardWriter(boardWriter).boardContent(boardContent).build());List<Board> boardList = boardRepository.findAll();Board board = boardList.get(boardList.size() - 1);System.out.println("==== ==== ==== ==== ==== ==== ==== ====");System.out.println("createdDate : " + board.getCreatedDate());System.out.println("modifiedDate : " + board.getModifiedDate());System.out.println("==== ==== ==== ==== ==== ==== ==== ====");assertThat(board.getCreatedDate()).isAfter(now);assertThat(board.getModifiedDate()).isAfter(now);}}cs Github Repository - https://github.com/tychejin1218/jpa-sample
반응형'Spring Boot > 2.3.x - JPA 게시판 만들기' 카테고리의 다른 글
[Spring Boot] JPA 게시판 만들기(2) - API 테스트 코드 구현 (1) 2020.07.20 [Spring Boot] JPA 게시판 만들기(1) - 기본 설정 및 CRUD 구현 (0) 2020.06.25