Question:What is flush mode in hibernate
Answer: As we already aware that hibernate maintains objects in cache before actually comitting a session.When we commit a session hibernate synchronizes cached data with the database. We can control this behaviour by setting the flushmode property by calling "session.setFlushMode()"
Hibernate provides us with three different flush modes
1.AUTO(FlushMode.AUTO) : By setting this mode we are actually leaving the option to hibernate like when to flush the data from session to database. Hibernate knows how to deal this. For example if we have updated some table and then when we query to the same table we should get the updated data. In this situations hibernate flushes the data before we actually execute the query since we need the updated data.
2.COMMIT(FlushMode.COMMIT) : This mode tells hibernate that flush the data from the cache at the end of every transaction.
3. NEVER(FlushMode.NEVER) : This mode allows flushing the data when we actually call the method Flush().
Based on our application requirements we have to set the flush mode.Nomrally people prefer not to touch the flush mode setting.Some times if we use COMMIT as the flush mode then we may end up with bad data which causes dirty reads.
Hibernate provides us with three different flush modes
1.AUTO(FlushMode.AUTO) : By setting this mode we are actually leaving the option to hibernate like when to flush the data from session to database. Hibernate knows how to deal this. For example if we have updated some table and then when we query to the same table we should get the updated data. In this situations hibernate flushes the data before we actually execute the query since we need the updated data.
2.COMMIT(FlushMode.COMMIT) : This mode tells hibernate that flush the data from the cache at the end of every transaction.
3. NEVER(FlushMode.NEVER) : This mode allows flushing the data when we actually call the method Flush().
Based on our application requirements we have to set the flush mode.Nomrally people prefer not to touch the flush mode setting.Some times if we use COMMIT as the flush mode then we may end up with bad data which causes dirty reads.