审核(@CreatedDate)在Spring Boot Cassandra中不起作用

时间:2019-10-23 17:21:03

标签: spring spring-boot cassandra spring-data

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

@CreatedDate
private LocalDateTime createdDate;

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

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

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

1 个答案:

答案 0 :(得分:0)

根据文档说:

  

我们提供@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 does not work answer