首页 > 解决方案 > 审计(@CreatedDate)不适用于 Spring Boot Cassandra

问题描述

我有一个简单的实体,我想让“createdDate”自动分配。

@CreatedDate
private LocalDateTime createdDate;

正如文档中所描述的,我还在我的 CassandraConfig 中添加了注释“ @EnableCassandraAuditing ”:

@Configuration
@EnableCassandraRepositories
@EnableCassandraAuditing
public class CassandraConfig extends AbstractCassandraConfiguration { 

但它仍然不起作用.. 实体是使用 createdDate=null 创建的。任何帮助表示赞赏。

标签: springspring-bootcassandraspring-data

解决方案


根据文档说:

我们提供@CreatedBy,@LastModifiedBy来捕获创建或修改实体的用户以及@CreatedDate捕获@LastModifiedDate 发生这种情况的时间点。

我认为最后一句话的意思是capture the point in time this happened by some user.自从我issue很久以前就有过同样的事情。

然后,如果您想@CreatedDate注释作品,那么您需要给当前的审核员。应该Auditor Aware通过以下方式或只是第二个示例来创建您的自定义。

第一个例子:

class SpringSecurityAuditorAware implements AuditorAware<User> {

    public Optional<User> getCurrentAuditor() {

        return Optional.ofNullable(SecurityContextHolder.getContext())
                 .map(SecurityContext::getAuthentication)
                 .filter(Authentication::isAuthenticated)
                 .map(Authentication::getPrincipal)
                 .map(User.class::cast);
    }
}

第二个例子:

class SpringSecurityAuditorAware implements AuditorAware<String> {

    public Optional<String> getCurrentAuditor() {
         return Optional.of("CurrentUser");
    }
}

笔记

我找到了一个类似的答案,您可以检查@CreatedDate 不起作用的答案


推荐阅读