views:

59

answers:

2

When we are updating a record, we can use session.flush() with Hibernate. What's the need for flush()?

+1  A: 

Flushing the Session gets the data that is currently in the session synchronized with what is in the database.

More on the Hibernate website:

flush() is useful, because there are absolutely no guarantees about when the Session executes the JDBC calls, only the order in which they are executed - except you use flush().

The MYYN
+1  A: 

Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database (i.e. to write changes to the database). Hibernate will flush changes automatically for you:

  • before query executions
  • when a transaction is committed

Allowing to explicitly flush the Session gives finer control that may be required in some circumstances (to get an ID assigned, to control the size of the Session,...).

Pascal Thivent