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