Unlike a POST, where you can reconstruct the entire entity snapshot when using PUT, you need to specify what property you want to update.
Now, in a @Transactional
method, you need to:
-
Fetch the entity:
Post post = entityManager.find(Post.class, postId);
-
Set the entity property using the value you submitted via PUT:
post.setTitle(title);
If you want to make it dynamic, you can use Java Reflection to set the entity property by matching the provied proeprty name.
When the current transaction is committed, a flush will occur and propagate the entity property change to the database.
That’s it.