I have two tables to store answers and HTML template of a form. Table answers has foreign key whih refers to primary key in html_templates table. There is a problem when I try to add a record to answer table.
//session opened
Answers answers = new Answers();
answers.setAnswer("answer...");
//there is a record in getHtmlTemplates with 4 as an id
answers.setHtmlTemplates(getHtmlTemplates(4));
session.save(answers);
Program executes without any warrings, but there is no record in DB (MySql). When I am adding a record to answers manually, the id is incremented by more than one. Actually when all operations are within transaction a record is added. What happened? It looks like DB database has deleted this record.
It’s likely that you are not committing the transaction. For auto-increment columns like MySQL uses, the
savemethod causes a dummy record to be inserted so that Hibernate can retrieve the new ID value (notice thatsavereturns an object, which is the new ID). MySQL has now marked that value as being used, which is why it looks like the ID has “jumped” when you manually insert.