According to my understanding in hibernate (please confirm)
1- You have to session.close()
if you get it by getSessionFactory().openSession()
.
2- No need to session.close()
if you get it by getSessionFactory().getCurrentSession()
. It is automatically closed after commit().
3- @2 When using getSessionFactory().getCurrentSession()
, we have to do all DB activities inside an active transaction so that we can commit() at the end.
4- Hibernate en-queues all save, update, and delete operations and submits them to the database server only after a flush() operation or committing the transaction or closing of the session in which these operations occur.(as per javadoc)
From the above points if I consider 1 & 4, then the following code should work:
Session session = HibernateUtil.getSessionFactory().openSession();
AccountDetails ac = new AccountDetails();
//perform set operations
session.save(ac);
session.close();
System.out.println("new account stored.");
BUT it is not working i.e. it runs smoothly but does not reflect(store) in database.Why this is so ? When I write the code inside a transaction and commit, then it is stored.
I think I am missing a basic thing. Please clarify.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…