Hibernate: Bulk update bypasses the persistence context

It is fast, but entities already loaded keep their old values until you clear the context.

Code
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("update Order o set o.status = :to where o.status = :from")
int bulkTransition(@Param("from") Status from, @Param("to") Status to);
Output
update orders set status=? where status=?   -> 4213 rows in one statement

Without clearAutomatically, an Order already in the session
still reports the OLD status.
Advertisement
More in HIBERNATE

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11

© Java Coding Hub · About · Contact · Privacy · Terms