Java @CreatedBy在Spring数据JPA中是如何工作的?

Java @CreatedBy在Spring数据JPA中是如何工作的?,java,spring,jpa,spring-data,spring-data-jpa,Java,Spring,Jpa,Spring Data,Spring Data Jpa,我在实体属性上使用了@CreatedDate,我看到它将日期插入到数据库中。我不明白Spring Data JPA中的@CreatedBy注释的目的是什么 在报告中,我读到: 我们提供@CreatedBy,@LastModifiedBy来捕获创建或修改实体的用户 但是如何创建和使用这样的用户呢?如果您已经阅读了参考文档,我建议您阅读以了解有关和如何使用AuditorAware的信息() TL;博士 你的实体 @CreatedBy private UUID modifyBy; 还有你的安全审计软

我在实体属性上使用了
@CreatedDate
,我看到它将日期插入到数据库中。我不明白Spring Data JPA中的
@CreatedBy
注释的目的是什么

在报告中,我读到:

我们提供
@CreatedBy
@LastModifiedBy
来捕获创建或修改实体的用户


但是如何创建和使用这样的用户呢?

如果您已经阅读了参考文档,我建议您阅读以了解有关和如何使用
AuditorAware的信息(

TL;博士

你的实体

@CreatedBy
private UUID modifyBy;
还有你的安全审计软件

@Component
public class SecurityAuditorAware implements AuditorAware<UUID> {

    @Override
    public Optional<UUID> getCurrentAuditor() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication == null || !authentication.isAuthenticated()) {
            return Optional.empty();
        }

        return Optional.of(((MyCustomUser) authentication.getPrincipal()).getId());
    }
}
@组件
公共类安全AuditorAware实现AuditorAware{
@凌驾
公共可选getCurrentAuditor(){
身份验证=SecurityContextHolder.getContext().getAuthentication();
if(authentication==null | |!authentication.isAuthenticated()){
返回可选的.empty();
}
返回可选的.of(((MyCustomUser)authentication.getPrincipal()).getId());
}
}
注:
您需要使用相同的数据类型,我使用UUID作为自定义用户id。

MyCustomUser来自哪里?@Chris它来自您的自定义用户工具,请检查