superman
superman

Reputation: 9

Hibernate is not saving data?

My session is working but not saving data in database ..What should I missing..?

enter image description here

Upvotes: 0

Views: 34

Answers (1)

Mehmood Memon
Mehmood Memon

Reputation: 1149

You have not opened any transaction so the changes won't be reflected in 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.

Example:

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();
 }

Either use Transaction to save the object or in current scenario make the following change in your configuration file:

 <property name="connection.autocommit">true</property>

Upvotes: 1

Related Questions