Skip to main content

flush(), clear(), refresh(), detach() and Legacy Operations

Learn flush(), clear(), refresh(), detach() and Legacy Operations (Phase 3 · CRUD Operations (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

flush() and clear() • flush() synchronises the persistence context with the database: it executes pending INSERT/UPDATE/DELETE statements (inside the current transaction, not committed yet ). It happens automatically before commit and (in FlushMode.AUTO ) before a query that could be affected. • clear() detaches all managed entities and empties the first-level cache without flushing: unflushed changes are lost. Typical use: batch processing : flush(); clear(); every N rows to keep memory flat. • commit() = flush + commit of the database transaction (changes become durable and visible).

More lessons in Phase 3 · CRUD Operations (Essential)