Java Spring启动审核-将当前用户映射到@CreatedBy、@LastModifiedBy

Java Spring启动审核-将当前用户映射到@CreatedBy、@LastModifiedBy,java,spring,spring-boot,hibernate,jpa,Java,Spring,Spring Boot,Hibernate,Jpa,因此,我有一个使用@MappedSuperClass的审计类,它更新createdBy和updatedBy的值,但它没有在用户实体上添加外键,因此没有数据库验证 这是审计课 @吸气剂 @塞特 @映射超类 @JsonIgnoreProperties( 值={“createdBy”,“updatedBy”}, allowGetters=true ) 公共抽象类UserDateAudit扩展了DateAudit{ @创造的 @列(name=“创建人”,nullable=false,updateabl

因此,我有一个使用@MappedSuperClass的审计类,它更新createdBy和updatedBy的值,但它没有在用户实体上添加外键,因此没有数据库验证

这是审计课


@吸气剂
@塞特
@映射超类
@JsonIgnoreProperties(
值={“createdBy”,“updatedBy”},
allowGetters=true
)
公共抽象类UserDateAudit扩展了DateAudit{
@创造的
@列(name=“创建人”,nullable=false,updateable=false)
公共长期投资;
@最后修改
@列(name=“updated_by”,nullable=false)
公共长期更新者;
}
我搜索了如何使用@inheritation实现这一点,但没有完全理解。 那么如何实现这个类和用户实体之间的连接呢

编辑1 下面是我实现的审计配置

@配置
@启用JPA审核
公共类审核配置{
@豆子
公共AuditorAware auditorProvider(){
返回新的SpringSecurityAuditAwareImpl();
}
}
类SpringSecurityAuditAwareImpl实现AuditorAware{
@凌驾
公共可选getCurrentAuditor(){
身份验证=SecurityContextHolder.getContext().getAuthentication();
如果(身份验证==null)||
!身份验证。已验证()||
匿名身份验证实例(AuthenticationToken){
返回可选的.empty();
}
UserPrincipal UserPrincipal=(UserPrincipal)身份验证。getPrincipal();
返回可选的.ofNullable(userPrincipal.getId());
}
}
编辑2 弄清楚我的确切意思
那么,如何使用@MappedSuperclass以外的东西来实现这一点“我希望能够在继承UserDateAudit的所有表中映射用户引用,以便它是所有这些表的外键(这将添加用户id实际存在的验证),而不仅仅是一个常规列”.

您需要实现
org.springframework.data.domain.AuditorAware
接口,并在
getCurrentAuditor
方法中添加检索当前登录用户的逻辑

@Component
@RequiredArgsConstructor
public class AuditorResolver implements AuditorAware<YOUR_TYPE> {
 
    @Override
    public Optional<YOUR_TYPE> getCurrentAuditor() {

        //code to retrieve the currently logged in user and return the id/user object 
    }
}
@组件
@所需参数构造函数
公共类AuditorResolver实现AuditorAware{
@凌驾
公共可选getCurrentAuditor(){
//用于检索当前登录的用户并返回id/用户对象的代码
}
}
请参见以下示例:


文档还对其进行了漂亮的描述:

您需要实现org.springframework.data.domain.AuditorAware接口,并添加逻辑以在getCurrentAuditor方法中检索当前登录的用户。看一看example@OmarAbdelhady继承模型不在数据库级别进行镜像,
UserDateAudit
类的hance字段可以属于不同的表,具体取决于扩展它的目标实体。所以,你的问题不清楚。我知道@MappedSuperclass没有镜像,所以如何使用其他东西来实现这一点“使用什么来映射继承UserDateAudit的所有表中的用户引用,以便它是所有这些表的外键(这将添加用户id实际存在的验证)不仅仅是一个常规列。是的,这就是我要做的,我将把它包括在问题中。但我的问题是,在执行此操作时,如何将createdBy和updatedBy值映射到用户表,以使hibernate添加在每个子类型(实体)上引用用户表的外键关于超类,你看了示例代码了吗?看了,这就是我要找的,谢谢