Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate session.save() does not reflect in database

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.

like image 646
mukund Avatar asked Apr 29 '13 10:04

mukund


People also ask

What is the difference between session Save () and session persist () Me?

Difference between saving and persist method in Hibernate 1)The first difference between save and persist is there return type. Similar to save method, persist also INSERT records into the database, but return type of persist is void while return type of save is Serializable Object.

What is the return type of the save () method of session?

The save() method returns the generated identifier so it has to immediately execute the SQL INSERT statement (it does not matter if we are inside or outside of a transaction) because identifiers are generated by the database during the INSERT query execution only.

How Save method works in Hibernate?

Hibernate save method returns the generated id immediately, this is possible because primary object is saved as soon as save method is invoked. If there are other objects mapped from the primary object, they gets saved at the time of committing transaction or when we flush the session.

What is the difference between the Save () and saveOrUpdate () method of Hibernate?

Difference between save and saveOrUpdate in Hibernatesave() generates a new identifier and INSERT record into the database while saveOrUpdate can either INSERT or UPDATE based upon the existence of a record.


2 Answers

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();  } 
like image 138
Dmitry Kuskov Avatar answered Sep 28 '22 09:09

Dmitry Kuskov


I was also trying to understand the point: save() can perform Insert operation outside transaction boundary so I did something like this

SessionFactory factory = new Configuration().configure()                     .buildSessionFactory();             Session session = factory.openSession();              session.save(user);              session.close(); 

But data not inserted in database.So I again tried this and now it worked for me and data inserted sucessfully:

in configuration file:

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

and in java code:

    SessionFactory factory = new Configuration().configure()             .buildSessionFactory();     Session session = factory.openSession();      session.save(user);      session.flush();     session.close(); 
like image 43
Vibhor Bhardwaj Avatar answered Sep 28 '22 09:09

Vibhor Bhardwaj