I am using Spring data jpa for persistence. Say, I have to update a model. This model has 'n' number of fields along with a primary Key.
{
"some_model":{
"id":"5527716",
"field_one": "44248156",
"field_two": "44248156",
"field_three": "44248156",
"field_four": "44248156",
"field_five": "44248156",
"field_six": "44248156",
"field_seven": "44248156",
"field_eight": "44248156",
"field_nine":"65037768"
}
}
Considering above json as a representation of my model, I want to update only those fields which are incoming in the json (primary key is Id which will always be there). Is there any way I can achieve without having to check each field explicitly and setting them?
Say I get a request like:
{
"some_model":{
"id":"5527716",
"field_one": "44248156",
"field_two": "44248156",
"field_three": "44248156",
"field_eight": "44248156",
"field_nine":"65037768"
}
}
I want to achieve update (upsert kind of thing) just using repository.save or something similar. Currently using repository's save method internally updates the model using primaryKey value but sets the values for absent fields as null. I want those fields to remain unchanged.
Main problem is: there are hundreds of models and each model has around 10-15 fields and explicitly checking each field and updating for all the models would be a nightmare.
I also checked about @DynamicUpdate
annotation but it seems to be applicable for explicit setting where generated sql is optimized.
I would appreciate any ideas regarding this.