According to my understanding in hibernate (please confirm)
1- You have to session.close()
if you get it by getSessionFactory().openSession()
Whenever you carry out any unit of work on the database objects, Transactions have to be used. http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/transactions.html shows why they are used and how they can be used. But, the key is to use a transaction
object by calling session.beginTransaction()
which returns a Transaction
object. This will represent the unit of work carried out to the database.
When you create session using SessionFactory.openSession(), no transaction is created, so your operations are executed outside of transaction context. In order to see your changes, you have to start a new transaction, or perform your operations as a part of ongoing transaction. From documentation:
A typical transaction should use the following idiom:
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
You forgot to commit .You should to commit.
session.save(ac);
// you should to add this bottom line
session.getTransaction().commit();
Whenever Your are doing save(), Always commit the transaction once done.
Account ac =new account()
ac.insert(123);
----------
----------
----------
----------
session.save(ac);
// Add this bottom line
session.getTransaction().commit();
Yeah session.save
is not saved to database without have the transaction.
Only way to do it is by using the
<property name="connection.autocommit">true</property>
If this property is used you no need transaction and also you do not need session.flush()
or session.close()
I found I had to flush and then refresh the DAO, and then it worked.
session.flush()
session.refresh(entityDAO)