How to prevent Hibernate from updating column values as null in a PUT web request when the entity uses @DynamicUpdate

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:

  1. Fetch the entity: Post post = entityManager.find(Post.class, postId);

  2. 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.