java – Partial object update / Do not update null values ​​with JPA

Question:

I'm using JPA for a webservice, and I'm using Merge for updating, but it updates all the values ​​of my object, is there any way to do a partial update of it?

Example, only change values ​​that are not null ?

Answer:

JPA's merge operation will always synchronize all object state in the database. But there is a way to accomplish what you want:

  1. Load the original object from the database by Id using find() from the JPA session
  2. Use a library like BeanUtils to copy the non-null properties of the updated object partially produced by the service to the object loaded by find() . See how to do it here

JPA will generate the update but only the properties changed in the loaded object will have different values.

Scroll to Top
AllEscort