简体   繁体   中英

Saving data in database using hibernate and commit()

I am creating restful api in java EE.

I have defined

private static Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
private static org.hibernate.Transaction  tx = session.beginTransaction();

in class and in method i am calling

Person n  = new Person( getPersonId() , name);
          session.save(n); 
          tx.commit();

however commit() closes transaction , and without commit the data wont be saved in database. What is the optimal way to saving data in database without using commit? This leads to creting new transaction on every request which is slow.

Try not using transaction and commit. Use flush after save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM