为什么Hibernate Envers将Last_edited_by列值替换为“ API”

发布于 2025-02-02 05:24:21 字数 1691 浏览 2 评论 0原文

我有类似的类结构:

@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中包括了spring-data-jpaenvers依赖性。我明确设置了createbylastModifiedby列的值,但是每当发生插入发生createby and code> lastModifiedby 时列值在我们的Oracle DB中的表中设置为“ API”。同样,每当我执行更新lastModifiedby列值时,即使我将用户ID设置在实体中,也将设置为“ API”。为什么会发生这种情况?我是否缺少某些财产价值?

I have class structure something like:

@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;
}

I've been using hibernate envers to for auditing purpose in my spring-boot app. I have included spring-data-jpa and envers dependency in pom. I am explicitly setting values of createdBy and lastModifiedBy columns to user's id but whenever the insert is happening createdBy and lastModifiedBy column values are being set to "API" in the table in our Oracle DB. Also whenever I am performing update lastModifiedBy column value is being set to "API" even though I have set userid in my entity. Why is this happening? Am I missing some property value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2025-02-09 05:24:21

由于您使用的是弹簧数据,因此用@createdby @lastmodifiedby注释的实体将被当前记录的用户填充。
您可以覆盖此行为,并告诉Spring在数据库中插入新记录时存储当前的用户ID。为此,您将需要提供auditoraware接口和Override 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);
        }
    }

Since you are using Spring Data, entities annotated with @CreatedBy @LastModifiedBy are going to be populated with the currently logged user.
You can override this behaviour and tell Spring to store current user id whenever you insert new record in database. To achieve that you will need to provide an implementation of AuditorAware interface and override getCurrentAuditor() method. Inside getCurrentAuditor() you will need to fetch currently logged in user 's 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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文