Edit User Data
Explore how to securely edit user data in a Spring Boot application using Thymeleaf by implementing optimistic locking with version control. Learn to modify Java entities, update SQL schema, handle form data, and manage validation sequences to prevent concurrent data conflicts and ensure accurate user updates.
We'll cover the following...
Adding version
In order to support the editing of our User entities, we will need to make some changes in the Java code. First of all, we will use io.github.wimdeblauwe.jpearl.AbstractVersionedEntity as the superclass of User instead of io.github.wimdeblauwe.jpearl.AbstractEntity.
This class adds a version field to the entity, which will allow us to use user or by himself in another tab, for example).
Updating SQL script
Let’s update our SQL creation script to have the new version field:
On line 4, we have added the version field.
Edit user method
Next, we’ll create a new UserService method to allow user properties to be eidted:
Since we will allow the editing of all parameters used at creation, we can extend from CreateUserParameters and add the version field:
Now we’ll update UserServiceImpl by adding the following code:
- Get the user for the given
UserIdfrom the database. If there is no matching user, throw a