0

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.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
  • 3
    This might help you http://stackoverflow.com/a/8137376/507864 – ManuPK Apr 19 '12 at 14:24
  • @ManuPK good link thanks – Hussein Zawawi Apr 19 '12 at 14:29
  • @ManuPK: This is the best answer, but you filled in a "comment" and not an "answer". Add this as an Answer and I will certainly upvote it. – Randy Stegbauer Apr 21 '12 at 01:23
  • @RandyStegbauer : If the link in the answer is useful, you can upvote it. It won't be proper for me to copy that answer and paste for this question. in the meanwhile answer in the link is by me. – ManuPK Apr 22 '12 at 06:12
  • @ManuPK, I'm not sure I agree, but if that's the custom, then I go along with that. My issue is that this question *has* an answer, but it's not marked as answered. – Randy Stegbauer Apr 22 '12 at 12:00

3 Answers3

1

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.

kyiu
  • 1,926
  • 1
  • 24
  • 30
1

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().

Nitin Chhajer
  • 2,299
  • 1
  • 24
  • 35
0

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

maksimov
  • 5,792
  • 1
  • 30
  • 38