Hussein Zawawi
Hussein Zawawi

Reputation: 2927

Hibernate flush method

I have the following piece of code that inserts or updates a bean in the database. I have a static function in the HibernateUtil that returns a singleton instance from the Hibernate session.

hibSession = HibernateUtil.currentSession();
hibSession.saveOrUpdate(bean);
hibSession.flush(); 

This is existing code, I am wondering if there is any reason that make the programmer use flush instead of simply committing and what flush does exactly.

Upvotes: 0

Views: 1485

Answers (3)

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

flush is re-syncs the DB with Hibernate. It is useful if you have a trigger set on a table. The trigger will run on flush and does not need the transaction to commit().

Upvotes: 1

kyiu
kyiu

Reputation: 1976

The flush() method synchronizes modifications bound to the current persistence context with the underlying DB. The flush() method does not end the running transaction.

One concrete usage of the flush() method is to force database triggers or generator logic (generated ID, for instance) to execute.

Upvotes: 1

maksimov
maksimov

Reputation: 5811

flush synchronises the Hibernate session with the database, commit ends the database transaction.

Upvotes: 0

Related Questions