简体   繁体   English

mongodb 在 spring boot 中审计以保存 createdDate、lastModifiedDate、createdBy、lastModifiedBy

[英]mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBy

I am using spring boot, therefore I am not using any xml files for configurations.我正在使用 spring boot,因此我没有使用任何 xml 文件进行配置。 What I have to do is to EnableMongoAuditing for saving createdDate, lastModifiedDate etc while saving data using MongoRepositories.我要做的是启用MongoAuditing 以保存createdDate、lastModifiedDate 等,同时使用MongoRepositories 保存数据。

My model class我的模特班

@Component
@Document(collection = "CAPPING")
public class TemporaryCapping extends BaseEntity {

    @Field("contract_id")
    private BigInteger contractId;

    @Field("period_id")
    private BigInteger periodId;

    @Field("user_id")
    private BigInteger userId;

    @Field("amount")
    private Double amount;

    @Field("type_of_capping")
    private TypeOfCapping typeOfCapping;

    public BigInteger getContractId() {
        return contractId;
    }

    public void setContractId(BigInteger contractId) {
        this.contractId = contractId;
    }

    public BigInteger getPeriodId() {
        return periodId;
    }

    public void setPeriodId(BigInteger periodId) {
        this.periodId = periodId;
    }

    public BigInteger getUserId() {
        return userId;
    }

    public void setUserId(BigInteger userId) {
        this.userId = userId;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public TypeOfCapping getTypeOfCapping() {
        return typeOfCapping;
    }

    public void setTypeOfCapping(TypeOfCapping typeOfCapping) {
        this.typeOfCapping = typeOfCapping;
    }


}



public class BaseEntity implements Serializable{

@Id
@Indexed(unique = true)
private BigInteger id;

@CreatedDate
private DateTime createdDate;

@Field("modified_date")
private BigInteger modifiedDate;

public BigInteger getId() {
    return id;
}

public void setId(BigInteger id) {
    this.id = id;
}

public DateTime getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(DateTime createdDate) {
    this.createdDate = createdDate;
}

public BigInteger getModifiedDate() {
    return modifiedDate;
}

public void setModifiedDate(BigInteger modifiedDate) {
    this.modifiedDate = modifiedDate;
}

I have used @CreateDate annotation for saving createDate.我已经使用 @CreateDate 注释来保存 createDate。 and I have used jodatime dependency for DateTime我已经将 jodatime 依赖用于 DateTime

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>

spring-data-mongodb is also added in the dependencies. spring-data-mongodb 也添加在依赖项中。

This is my main application class这是我的主要应用程序类

 @SpringBootApplication
    @EnableMongoAuditing
   public class Application {

   public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
   }

} 

Where I am wrong in this impelmentation as the date is not getting saved in database?由于日期未保存在数据库中,因此我在此实施中的错误在哪里? Also I know that for saving @createdBy you need to write AuditorAware bean but for now I am just trying to save createdBy.我也知道为了保存@createdBy,你需要编写AuditorAware bean,但现在我只是想保存createdBy。

Where should @EnableMongoAuditing be used?应该在哪里使用@EnableMongoAuditing?

In my application I configure through Java code.在我的应用程序中,我通过 Java 代码进行配置。 I use @EnableMongAuditing this way and also create convertes for ZonedDateTime.我以这种方式使用 @EnableMongAuditing 并为 ZonedDateTime 创建转换。

@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = { BASE_PACKAGE })
public class MongoConfiguration extends AbstractMongoConfiguration {

    public static final String BASE_PACKAGE = "package.with.aggregates";

    @Value("${spring.data.mongodb.uri}")
    private String mongoUri;

    @Value("${spring.data.mongodb.database}")
    private String databaseName;

    @Override
    protected String getDatabaseName() {
        return databaseName;
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient(new MongoClientURI(mongoUri));
    }

    // Here you must add converters to Joda datetypes. In my solution is ZonedDateTime
    @Override
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<>();
        converterList.add(new DateToZonedDateTimeConverter());
        converterList.add(new ZonedDateTimeToDateConverter());
        return new CustomConversions(converterList);
    }

    @Override
    protected String getMappingBasePackage() {
        return BASE_PACKAGE;
    }
}

@EnableMongoAuditing 实际上可以放置在配置中的任何位置(@Configuration 注释旁边)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring jpa auditing lastmodifiedby和lastmodifiedDate都可以,但createdBy和createdDate注释总是为null - spring jpa auditing lastmodifiedby and lastmodifiedDate are ok, but createdBy and createdDate annotation are always null Spring 启动 - 实体:如何添加 CreatedDate(UTC 时区感知)、LastModifiedDate(UTC 时区感知)、CreatedBy、LastModifiedBy - Spring boot - Entity : How to add CreatedDate (UTC timezone aware), LastModifiedDate (UTC timezone aware), CreatedBy, LastModifiedBy Spring 启动审计 - map 当前用户到@CreatedBy,@LastModifiedBy - Spring Boot Auditing - map current user to @CreatedBy, @LastModifiedBy Spring Mongo 审计不工作@CreatedDate @CreatedBy - Spring Mongo Auditing not working @CreatedDate @CreatedBy Spring审核-Spring如何自动将@CreatedDate和@LastModifiedDate插入数据库? - Spring Auditing - How does spring insert @CreatedDate and @LastModifiedDate into database automaticaly? 审核(@CreatedDate)不适用于带有反应式 MongoDB 的 WebFlux Spring Boot - Auditing (@CreatedDate) does not work for WebFlux Spring Boot with reactive MongoDB 在事务方法中访问JPA审核LastModifiedBy / LastModifiedDate时为null - JPA Auditing LastModifiedBy/LastModifiedDate are null when being accessed in the transaction method Spring Data不通过@CreatedDate填充@LastModifiedDate - Spring Data fills @LastModifiedDate by not @CreatedDate Spring boot mongodb审计报错 - Spring boot mongodb auditing error Spring JPA 审计空 createdBy - Spring JPA Auditing empty createdBy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM