1

I have next code:

    Color color = new Color(null,"B12",null,1);

    Session session=sessionFactory.openSession();
    session.setFlushMode(FlushMode.MANUAL);

    session.beginTransaction();
    session.save(color);
    session.clear();

    session.getTransaction().commit();

    session.close();

    sessionFactory.close();

I just don't get it why data is stored to database when i specified that flush is manual? Please help me figure it out!

1 Answers1

1

The reason is session.getTransaction().commit() saves all changes to the database. See this stackoverflow post.

HIbernate commit() and flush()

Flush is used to push items to the database before the commit.

Community
  • 1
  • 1
darthNater
  • 65
  • 8
  • You see i read a book it's called "JAVA PERSISTENCE WITH HIBERNATE" Christian Bauer, Gavin King and here is a sentence from it: "By selecting FlushMode.MANUAL, you may specify that only explicit calls to flush() result in synchronization of managed state with the database". And in my code example there isn't explicit call to flush(), then why it's still stored to database, maybe there is something else i don't know? – Юра Арабчук Feb 21 '17 at 18:47
  • 1
    Normally calls to the database will result in a flush automatically. Flushmode.MANUAL would behave like the quoted sentence. Commit specifically means to save the changes to the database. This is describes it better http://stackoverflow.com/questions/14622962/what-is-transaction-commit-in-hibernate – darthNater Feb 21 '17 at 18:51
  • Thank's, i think i understand but still have little questian, i edited my code by edding session.clear() after session.save(). Documentation says about method clear() the following:"Evict all loaded instances and cancel all pending saves, updates and deletions". Why doesn't it cancel save() and still stores data to database? – Юра Арабчук Feb 21 '17 at 19:05