Hibernate: flush() sends SQL, commit() ends the transaction

Flushing writes pending statements so a later query sees them; it does not make them permanent.

Code
@Transactional
void demo() {
    em.persist(new Order("A-1"));
    // not in the DB yet
    em.flush();          // INSERT is sent
    // still inside the transaction - a rollback would undo it
    throw new IllegalStateException("boom");
}
Output
Hibernate: insert into orders ...
Then: rollback - the row never existed as far as anyone else is concerned.
Advertisement

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