Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving objects in database using hibernate

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to hibernate.
I read that in order to save our object first we need to call session.save(object) and then session.flush().

I did the same but found that no rows were inserted into the database.

After searching more I found that we can use transaction as Transaction tx=session.beginTransaction and after calling session.save if I call tx.commit()
then rows were inserted into the database.

I want to know that session.flush() is not going to insert anything into the database,then what is the use of it as in the transaction case i didn't use flush at all only transaction.commit().

Please help me.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Flush flushes the contents of the Session to the database, however something you need to understand about database (that is universal, it is not a Hibernate feature) is nothing is written to the database until the transaction you write it in is committed.

Flush must be called before committing the transaction. Depending on your flush-mode, this probably happens implicitly by calling commit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic