I have use case where I want to do a partial update of an object.
My object has the following fields and both fields are nullable.
My default update query looks like this:
@SqlUpdate("update user set name = :name, age = :age where id = :it")
public void updateUser(@Bind int id, @BindBean User user);
In my case the object can be partially updated from an API which means that if the name is updated but the age are not then the object looks like follows:
name = "Fredrik"
age = null
If I run the default update query the age field will be set to null, but it should not be updated at all (only the name field).
Unless there are some cool annotation that ignores NULL values I see two solutions.
1) Merge the two objects in my code and build the complete Object and then use the my default update query.
2) Create a custom binder which creates the query so it updates the fields which are not null with new values and updates the fields that are null with the same value.
The final query string would then be: "update user set name = "Fredrik", age = age where id = :it".
Though I don't know how to set the "age = age" part. In my example below with a custom binder the age would end up as a string (age = "age").
if(arg.getAge() != null)
q.bind("age", arg.getAge());
else
q.bind("age", "age);
Does any of you have any pointers how it could be solved without actually merging the two objects in my code.
Thanks!