簡體   English   中英

Spring 注釋@CreatedBy 和@CreatedByDate 不工作但@LastModifiedBy 和@LastModified 日期工作

[英]Spring Annotation @CreatedBy and @CreatedByDate is not working but @LastModifiedBy and @LastModified date is working

我正在嘗試創建一個文檔 class,我在其中使用各自的注釋添加字段 creationTime、LastmodificationTime、createdBy 和 LastModifiedBy。 我看到的是帶有注釋 @CreatedBy 和 @CreatedDate 的 class 變量被填充為 null 但帶有 @LastModifiedBy 和 @LastModifiedDate 的變量是。

這是實際 class 的代碼


import com.abcd.ops.cp.bo.BaseDBEntity;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Collection;
import java.util.Set;

@Document
@Getter
@Setter
@NoArgsConstructor
//@AllArgsConstructor
@Builder
public class RoleMapping extends BaseDBEntity {

    @Id
    private String roleName;

    private Set<RBACEntity> entities;

    private String description;

    private RoleType roleType = RoleType.SYSTEM_DEFINED;

    @Builder
    public RoleMapping(String roleName, Set<RBACEntity> entities, String description, RoleType roleType) {
        super(roleName);
        this.roleName = roleName;
        this.entities = entities;
        this.description = description;
        this.roleType = roleType;
    }
}

這是 BaseDBEntity.java

package com.abcd.ops.cp.bo;

import lombok.*;
import org.springframework.data.annotation.*;

import java.util.Date;

@Getter
@Setter
@NoArgsConstructor
public abstract class BaseDBEntity {

    @CreatedBy
    private String createdBy;

    @CreatedDate
    private Date creationDate;

    @LastModifiedDate
    private Date lastModifiedDate;

    @LastModifiedBy
    private String lastModifiedBy;
}

您需要添加一些注釋:

    package com.abcd.ops.cp.bo;
    
    import lombok.*;
    import org.springframework.data.annotation.*;
    
    import java.util.Date;
    
    @Getter
    @Setter
    @NoArgsConstructor
    @MappedSuperclass
    @EntityListeners(AuditingEntityListener.class)
    public abstract class BaseDBEntity {
    
        @CreatedBy
        private String createdBy;
    
        @CreatedDate
        private Date creationDate;
    
        @LastModifiedDate
        private Date lastModifiedDate;
    
        @LastModifiedBy
        private String lastModifiedBy;
    }

而且你還需要一個配置 class 像:

    @Configuration
    @EnableJpaAuditing(auditorAwareRef = "auditorProvider")
    class JpaAuditingConfig {
    
        @Bean
        foo auditorProvider(): AuditorAware<String> {
            return AuditorAware { Optional.of(SecurityContextHolder.getContext().authentication.name) }
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM