我有類似的類結構:
@Entity
@Data
@Table(name="person_dtl")
@Audited
@AuditTable(value="h$person_dtl")
public class Person extends AuditBaseEntity implements Serializable {
@Id
@Column(name="person_ID", nullable = false)
private String personID;
@Column(name="person_Name", nullable = false)
private String personName;
}
@Audited
@MappedSuperClass
@EntityListeners(AuditingEntityListener.class)
public abstract class AuditBaseEntity extends BaseEntity {
@Column(name = "created_by", nullable = false, updatable = false)
@CreatedBy
protected String createdBy;
@Column(name = "last_modified_by")
@LastModifiedBy
private String lastModifiedBy;
@Column(name = "created_date", nullable = false, updatable = false)
@CreatedDate
protected Date createdDate;
@Column(name = "last_modified_date")
@LastModifiedDate
private Date lastModifiedDate;
@PrePersist
public void onPrePersist() {
this.activeIndicator="A";
}
@PreRemove
public void onPreRemove() {
this.activeIndicator="I";
}
}
@Audited
@MappedSuperClass
public abstract class BaseEntity {
@Column(name = "active_ind", nullable = false)
@CreatedBy
protected String activeIndicator;
}
我一直在使用 hibernate envers 在我的 spring-boot 應用程式中進行審計。我在 pom.xml 中包含spring-data-jpa和envers依賴。我明確地將值createdBy和lastModifiedBy列設定為用戶 ID,但是每當插入發生createdBy并且lastModifiedBy列值在我們的 Oracle DB 的表中設定為“API”時。此外,每當我執行更新lastModifiedBy列值時,即使我在我的物體中設定了用戶 ID,也會將其設定為“API”。為什么會這樣?我錯過了一些財產價值嗎?
uj5u.com熱心網友回復:
由于您使用的是 Spring Data,因此注釋的物體@CreatedBy @LastModifiedBy 將填充當前登錄的用戶。您可以覆寫此行為并告訴 Spring 在您在資料庫中插入新記錄時存盤當前用戶 ID。為此,您需要提供AuditorAware介面和覆寫getCurrentAuditor()方法的實作。在里面getCurrentAuditor()你需要獲取當前登錄用戶的 id。
@Component
public class SpringSecurityAuditorAware implements AuditorAware<String> {
@Override
public String getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
MyUserDetails customUser = (MyUserDetails)authentication.getPrincipal();
int userId = customUser.getUserId();
return String.valueOf(userId);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483674.html
上一篇:從1.4.200更新到2.1.212后H2自動增量不起作用
下一篇:HibernateSearch6中FullTextQuery.setCriteriaQuery()的替代品是什么?
