Home > Back-end >  What is the best way of updating data in SpringBoot?
What is the best way of updating data in SpringBoot?

Time:02-10

For put request, Do I always have to check the old data and change the changed fields in order to update the existing data? Is it the right way to check for each data change?

CodePudding user response:

I do not know of any project which takes the effort to only update fields that were actually changed.

Usually what you'd do is that you just override all fields in your table with the new value as this is the easiest and most reliable way of doing so. Also consider, that custom logic that decides what to update also needs to be maintained and can have bugs. If you end up having a bug in that logic, most likely, you'll realize that you have data consistency errors which might be unfixable.

Most likely, when you use Spring Boot, you would probably also use Spring Data JPA and Hibernate which are going to take care of mapping your objects to your database. In that case, Hibernate is going to decide on the update strategy you use anyways.

If you are worried about data consistency and concurrent updates to the same record, I would recommend looking into Optimistic Locking, which is an easy way to handle that issue. It's very easy to setup by just adding a version column to your table.

  • Related