DEV Community

Cover image for Hibernate - Flush
Yiğit Erkal
Yiğit Erkal

Posted on

5 1

Hibernate - Flush

One of the most confusing points about JPA/Hibernate is the flush operation and its relation to the transaction.

Flush is the operation that transfers the changes related to the existing entities to the database with SQL statements in the PersistenceContext managed via Session in Hibernate and EntityManager in JPA.

Transaction, on the other hand, ensures that persistence operations (insert, update or delete) performed in the database between the beginning and the end (commit) become permanent with the “all-or-nothing” logic.

When working with JPA/Hibernate, the SQL equivalents of persist, merge, and remove/delete operations on the PersistenceContext are not immediately reflected in the DB. These changes are accumulated in the PersistenceContext. This is called the transactional write behind feature of the ORM.

These accumulated changes are reflected to the DB with the flush operation. However, in order for these changes to become permanent, the current transaction must be successfully terminated (commit).

Do I need flush after every operation? 💧

Flush operation is run automatically in the background when the transaction is committed under normal conditions. Normally there is no need to run a manual flush on the PersistenceContext.

With FlushMode, you can control when the flush operation will run.

JPA has

  • AUTO
  • COMMIT

Hibernate has

  • AUTO
  • COMMIT
  • MANUAL
  • ALWAYS

modes. By default, FlushMode is AUTO. In this case, the flush operation is automatically triggered immediately before the ORM queries run regarding the entities that have changed on the PersistenceContext and during the transaction commit.

To sum up
If the Flush method is run manually at any stage, changes on the PersistenceContext are detected and reflected in the database as INSERT, UPDATE, DELETE SQL statements. However, these changes can become permanent only if the transaction commits.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI Live!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️