How to get old entity value in @HandleBeforeSave event to determine if a property is changed or not?

后端 未结 10 1735
余生分开走
余生分开走 2020-12-13 13:33

I\'m trying to get the old entity in a @HandleBeforeSave event.

@Component
@RepositoryEventHandler(Customer.class)
public class CustomerEventHan         


        
10条回答
  •  死守一世寂寞
    2020-12-13 14:12

    I had exactly this need and resolved adding a transient field to the entity to keep the old value, and modifying the setter method to store the previous value in the transient field.

    Since json deserializing uses setter methods to map rest data to the entity, in the RepositoryEventHandler I will check the transient field to track changes.

    @Column(name="STATUS")
    private FundStatus status;
    @JsonIgnore
    private transient FundStatus oldStatus;
    
    public FundStatus getStatus() {
        return status;
    }
    public FundStatus getOldStatus() {
        return this.oldStatus;
    }
    public void setStatus(FundStatus status) {
        this.oldStatus = this.status;
        this.status = status;
    }
    

    from application logs:

    2017-11-23 10:17:56,715 CompartmentRepositoryEventHandler - beforeSave begin
    CompartmentEntity [status=ACTIVE, oldStatus=CREATED]
    

提交回复
热议问题