0

I want to record a event as soon as Status is updated/created. Right now I am using JPA, AbstractAuditEvent, IDiffEvent events for generating events where it I am successfully able to log complete entity information like Case create event and case updated event but want to log case status change event and audit only status column information separately how do I achieve that.

@Entity(name = "Case")
@EntityListeners(EntityLifecycleListener.class)
public class  Case{

    @Id
    @Column(name = "ID", updatable = false, nullable = false)
    @GeneratedValue(generator = "CASE_ID_SEQ")
    Long id;
    
    @Column(name = "NAME", unique = false, updatable = true, nullable = false)
    private String name;

    @Column(name = "NUMBER", unique = true, updatable = false, nullable = false)
    private String number;

    @Column(name = "STATUS", unique = false, updatable = true, nullable = false)
    String status;

    @Column(name = "UPDATED_BY", updatable = true, nullable = true)
    private String updatedBy;

    @Column(name = "UPDATE_DATE", updatable = true, nullable = true)
    private LocalDateTime updateDate;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public LocalDateTime getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(LocalDateTime updateDate) {
        this.updateDate = updateDate;
    }
}
ssl
  • 115
  • 1
  • 10
  • A `@PrePersist` listener? A database trigger? – Alan Hay Oct 05 '17 at 08:12
  • If I get the question right (it is not very clear) an idea would be to mark the current value of the `status` in a `@PostLoad` listener and then emit the event in a `@PrePersist` listener. The value of status when the entity is loaded could be recorded in a `transient` field of the entity itself for convenience, or in a map(entity id -> status) in the component handling the lifecycle events. – Nikos Paraskevopoulos Oct 05 '17 at 08:21
  • The `AbstractAuditEvent` and `IDiffEVent` terms are unfamilar to me wrt hibernate-envers. What are they related to? – Naros Oct 05 '17 at 14:20
  • @Naros : those are events are provided by hibernate to audit the tables via entity life cycle listener. – ssl Oct 06 '17 at 13:18
  • @Naros : those are events are provided by hibernate to audit the tables via entity life cycle listener. – ssl Oct 06 '17 at 13:18
  • I fixed the issue by creating new entity called status history and set one to many relationship between case and statushistory. – ssl Oct 06 '17 at 13:19
  • @ssl I'm glad you got it working, but to clarify, those event names are not part of `Hibernate` nor `Hibernate-Envers`; hence my confusion. We deal with events such as `PostInsertEvent`, `PostUpdateEvent`, etc. So unless those are being raised by some spring-data integration or some other middleware, I'm not sure how this related to `hibernate-envers` at all. – Naros Oct 06 '17 at 15:35

0 Answers0