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:
- Load the original object from the database by
Id
usingfind()
from the JPA session - Use a library like
BeanUtils
to copy the non-null properties of the updated object partially produced by the service to the object loaded byfind()
. See how to do it here
JPA will generate the update but only the properties changed in the loaded object will have different values.