over 2 years ago

資料版本記錄

Spring 自動建立 Version, CreatedDate, LastModifiedDate, CreatedBy, LastModifiedBy 等版本記錄屬性於 data 中

  • @Version: 該筆資料的版本,沒有版本控制
  • @CreatedDate: 資料建立時間
  • @LastModifiedDate: 資料最後修改時間
  • @CreatedBy: 資料建立者
  • @LastModifiedBy: 資料最後修改者

CreatedBy, LastModifiedBy 需要自行實作稽核員 - AuditorAware。
實作AuditorAware 時可另外透過 Security 模組中的 Authentication 取得登入者資訊,方可寫入 CreatedBy, LastModifiedBy 中

建立好 BaseEntity 後,其他資料類別接繼承 BaseEntity

Dependency

pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>1.2.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.12.6</version>
</dependency>

Example

BaseEntity.java
import java.util.Date;

import lombok.Getter;
import lombok.Setter;

import org.joda.time.DateTime;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Version;

public class BaseEntity  {

    @Id
    @Getter @Setter
    protected String id;
    
    @Version
    @Getter @Setter
    protected Long version;  // don't use 'long'

    @CreatedDate
    @Getter @Setter
    protected Date created;
    
    @LastModifiedDate
    @Getter @Setter
    protected Date lastModified;
    
    @CreatedBy
    @Getter @Setter
    protected String createBy;   // who create, will retrieved from your application's SecurityContext 

    @LastModifiedBy
    @Getter @Setter
    protected String lastModifiedBy; // who update, will retrieved from your application's SecurityContext

}
UserNameAuditor.java
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import edu.tcu.mi.emrie.model.security.GaduoUserDetails;


public class UserNameAuditor implements AuditorAware<String> {

    @Override
    public String getCurrentAuditor() {
        return "SystemByDefault";
        
        /* ==== 另外實作 Spring-Security ==== */
        /*Authentication authentication = SecurityContextHolder.getContext().getAuthentication();        
        if (authentication == null || !authentication.isAuthenticated()) {
            return "SystemByDefault";
        } 
        return ((GaduoUserDetails) authentication.getPrincipal()).getUsername();*/
        
    }
}

business-context
    <mongo:auditing auditor-aware-ref="UserNameAuditor"/>
    <beans:bean id="UserNameAuditor" class="edu.tcu.mi.emrie.model.UserNameAuditor" />

資料參考來源:

  1. https://github.com/spring-projects/spring-data-mongodb
  2. http://www.javabydefault.com/2014/03/how-to-set-createdby-createddate.html
  3. http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-auditing-part-two/
← Java - lombok - 消除冗贅程式碼 MachineLearning - Tensorflow 安裝 →
 
comments powered by Disqus