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

后端 未结 10 1731
余生分开走
余生分开走 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:20

    Just another solution using model:

    public class Customer {
    
      @JsonIgnore
      private String name;
    
      @JsonIgnore
      @Transient
      private String newName;
    
      public void setName(String name){
        this.name = name;
      }
    
      @JsonProperty("name")
      public void setNewName(String newName){
        this.newName = newName;
      }
    
      @JsonProperty
      public void getName(String name){
        return name;
      }
    
      public void getNewName(String newName){
        return newName;
      }
    
    }
    

    Alternative to consider. Might be reasonable if you need some special handling for this use-case then treat it separately. Do not allow direct property writing on the object. Create a separate endpoint with a custom controller to rename customer.

    Example request:

    POST /customers/{id}/identity
    
    {
      "name": "New name"
    }
    

提交回复
热议问题