Spring JPA 는 @ CreatedDate, @ CreatedBy, @ LastModified Date, @ LastModified By 를 사용 하여 자동 으로 시간 과 수정 자 를 생 성 합 니 다.

JPA Audit
spring jpa 에 서 는 필드 나 방법 에 대한 주해 @CreatedDate, @CreatedBy, @LastModifiedDate, @LastModifiedBy 를 지원 합 니 다.
  • @CreatedDate 이 필드 는 시간 필드 를 만 드 는 것 으로 이 실체 가 insert 에 있 을 때 값
  • 을 설정 합 니 다.
  • @CreatedBy 이 필드 는 창설 자 를 나타 내 며, 이 실체 가 insert 에 있 을 때 값 을 설정 합 니 다
  • @LastModifiedDate, @LastModifiedBy 같은 이치 입 니 다.

  • 어떻게 사용 합 니까?
    먼저 실체 류 를 밝 히 려 면 클래스 에 주해 @EntityListeners(AuditingEntityListener.class) 를 더 해 야 하고, 애플 리 케 이 션 시작 클래스 에 주해 EnableJpaAuditing 를 더 해 야 하 며, 필요 한 필드 에 @CreatedDate, @CreatedBy, @LastModifiedDate, @LastModifiedBy 등 주 해 를 더 해 야 한다.
    이 때 jpa. save 방법 이 호출 될 때 시간 필드 는 자동 으로 데이터 베 이 스 를 설정 하고 삽입 합 니 다. 그러나 Created By 와 LastModified By 는 값 을 부여 하지 않 았 습 니 다. AuditorAware 인 터 페 이 스 를 실현 해 야 하기 때문에 삽입 할 값 을 되 돌려 줍 니 다.
  • Application
  • import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.context.annotation.Import;
    import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
    
    
    @SpringBootApplication
    @EnableJpaAuditing
    public class WalletApplication {
        public static void main(String[] args) {
            new SpringApplicationBuilder(WalletApplication.class).web(true).run(args);
        }
    }
    
  • AuditorAware
  • import org.springframework.context.annotation.Configuration;
    import org.springframework.data.domain.AuditorAware;
    import org.springframework.security.core.context.SecurityContext;
    import org.springframework.security.core.context.SecurityContextHolder;
    
    @Configuration
    public class UserIDAuditorBean implements AuditorAware {
        @Override
        public Long getCurrentAuditor() {
            SecurityContext ctx = SecurityContextHolder.getContext();
            if (ctx == null) {
                return null;
            }
            if (ctx.getAuthentication() == null) {
                return null;
            }
            if (ctx.getAuthentication().getPrincipal() == null) {
                return null;
            }
            Object principal = ctx.getAuthentication().getPrincipal();
            if (principal.getClass().isAssignableFrom(Long.class)) {
                return (Long) principal;
            } else {
                return null;
            }
        }
    }
    
  • Entity
  • import java.util.Date;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.EntityListeners;
    import javax.persistence.Table;
    
    import org.springframework.data.annotation.CreatedDate;
    import org.springframework.data.annotation.LastModifiedDate;
    import org.springframework.data.jpa.domain.support.AuditingEntityListener;
    
    
    /**
     *            .
     * @author Wang.ch
     *
     */
    @Entity
    @Table(name = "store_source_bind")
    @EntityListeners(AuditingEntityListener.class)
    public class StoreSourceBind {
        /**
         *     
         */
        @Column(name = "create_time")
        @CreatedDate
        private Date createTime;
        /**
         *    
         */
        @Column(name = "create_by")
        @CreatedBy
        private Long createBy;
        /**
         *     
         */
        @Column(name = "lastmodified_time")
        @LastModifiedDate
        private Date lastmodifiedTime;
        /**
         *    
         */
        @Column(name = "lastmodified_by")
        @LastModifiedBy
        private String lastmodifiedBy;
    }
    

    좋은 웹페이지 즐겨찾기