简体   繁体   中英

Hibernate do not save my records in the database but the primary key is incremented

Hibernate do not save my records in the database but the auto increment primary key is incremented.

I have 5 users and each user has 10 records in mysql table X. Therefore there should be 50 records altogether. But there are only 40 and there is no error shown. Records of user 3 is not there in the database. But the primary keys go from 1 to 50. Meaning it goes from 1-20 and then start from 30 to 50. 21,22,23....etc are missing where the records of user 3 should have been

My application log says that hibernate save method is called without an exception. Any ideas why this might have happened? I do not have mysql binary log enabled.

My best guess is that you are not handling transaction properly If you are using Spring then add @Transactional annotation on your save method.

If it's plain java code then do manage transaction like this

try {  
    session = sessionFactory.openSession();  
    tx = session.beginTransaction();  
    // save operation      
    tx.commit();  

} catch (Exception ex) {  
  ex.printStackTrace();  
  tx.rollback();  
} finally {
  session.close();
}  

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