0

在我的存储库中,我有字段,其中注有@CreatedBy@CreationTimestamp。我使用spring-security进行身份验证。一切正常,DB中的字段填写正确的用户名creationTimestamp。但有时我需要自己设置创建的列,我需要覆盖@CreatedBy注释。如何实现这一目标?Spring AuditorAware集手工创建

回答

0

用户ID可以通过手动使用以下方法进行设置:

@PrePersist 
protected void onCreate() { 
    //retrieve userid here and set it. 
    String userUid = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    setCreatedBy(userUid); 
} 

@PreUpdate 
protected void onUpdate() { 
    //retrieve userid here and set it. 
    String userUid = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    setUpdatedBy(userUid); 
} 
+0

那么这个问题是,该用户ID为每一次不同。 @PrePersist中没有参数。如何将参数传递给它? – JamesLinux