spring - 审计(@CreatedDate)不适用于 Spring Boot Cassandra
问题描述
我有一个简单的实体,我想让“createdDate”自动分配。
@CreatedDate
private LocalDateTime createdDate;
正如文档中所描述的,我还在我的 CassandraConfig 中添加了注释“ @EnableCassandraAuditing ”:
@Configuration
@EnableCassandraRepositories
@EnableCassandraAuditing
public class CassandraConfig extends AbstractCassandraConfiguration {
但它仍然不起作用.. 实体是使用 createdDate=null 创建的。任何帮助表示赞赏。
解决方案
根据文档说:
我们提供
@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 不起作用的答案
推荐阅读
- perl - CentOS8下如何安装perl DateTime模块。yum install "perl(DateTime)" 在 CentOS7 中有效,但在 8 中无效
- python - Python POST 请求返回“响应 400”
- python - 找不到 Spacy 法国模特
- sql-server - 将数据批量加载到使用 BCP 从 SQL Server 导出的 Snowflake 时出现 UTF-8 错误
- r - ggpairs 中的独立图例
- python - ValueError:检查目标时出错:预期 avg_pool 有 4 个维度,但得到了形状为 (100, 2) 的数组
- python - 在 Python 中查找两个日期之间的年差,余数以天表示
- sql - 如何在将其中之一设置为 DISTINCT 时选择多个列
- kotlin - 如何以内存高效的方式使用 Kotlin 的序列和 lambda
- r - 使用 bb_lookup 查找多个位置的边界框