Save selective updates in Spring Data JPA

in the use of Spring Data JPA, we often use the save method instead of updating. Doing so updates all fields, even the empty fields in the object, resulting in data loss. Although, JPA provides my own way to write SQL, but for updates, if I only want to update a field in the object, do I have to write a lot of updated interfaces? Ask the bosses for advice.

Mar.19,2021

put the field name field value to be updated in a Map and update it uniformly with a general update method, so you don't have to write an update method for each field:

public void update(Integer id, Map<String, Object> data) {
  ...
}
I have also encountered the problem of

. In addition to custom sql, there is a solution that does not write sql. This solution is to first find the entity, then use a tool class to reflect that the non-empty fields in the entity passed from the front end are stored in the collection, and then send the attribute copy of the found entity to the passed data (BeanUtils can Filter the reflected collection of non-empty fields). After that, you can save the transmitted data

Menu