I am trying to update a single property using PUT method. I am using DynamicUpdate annotation in Entity. I am facing the following issue. Consider I have 5 columns in a table and all values are inserted, In PUT request if I pass only one value as JSON Request, it is updating the remaining 4 columns with null… How to avoid/configure DynamicUpdate to not to update if any null values are being passed in PUT request.
Note: Updatable as false cannot be included in the entity since all fields can be passed in PUT request at different instances.
Thanks Vlad. My entity is of complex type as it contains relationship like @OneToMany which again contains @OneToMany relationship.
Other than manual setting up of the fields/using bean mappers is there anyway to notify spring not to update the values to null if the values are already existing in DB. In my case comparing each field/setting it manually will be a hectic task.
Other than manual setting up of the fields/using bean mappers is there any way to notify spring not to update the values to null if the values are already existing in DB.
You should retain the previously fetched entity that you sued to render the UI. Afterward, in the PUT request, you can let Spring change the entity based on the provided PUT request.
Afterward, you just use merge, assuming that you are using CascadeType.MERGE for child associations. This way, you no longer have to manually locate and set each prperty that gets modified.
Can we do it without fetching the entity first? In other words is there a JPA/Hibernate way to instruct it to update the record based on the provided attributes leaving the null ones untouched?
What if post = entityManager.find(Post.class, postId); is replaced with post = entityManager.getReference(Post.class, postId); are they equivalent in terms of number of sql queries?