I have the below code which tries to save the entity outside the transaction:
Session session = HibernateUtil.buildSessionFactory().openSession();
Teacher t= new Teacher();
t.setName("jonathan");
session.save(t);
session.flush();
According to the definition of save()
if used outside the transaction we need to do flush()
to save the entity in the db.However the above code does not save it. I have to create a transaction
and commit
it in order to add a row in db. Why is this so?
I am using HIbernate 4.3.6
EDIT: I just noticed that the session.save() returns the id but only after transaction.commit() the row is added.The other question does not answer my question.Since this basically means that save() in order to add the row in db has to be in a transaction only