Tuesday, September 11, 2007

Hibernate and Struts - Identifier Altered

In our application, we used Hibernate 3.1.2, Spring 1.2.6 and Struts 1.2.4. One of the page flows was to gather input from a form and insert a record into database then go back to the list page.

We saw messages like that,
org.hibernate.HibernateException: identifier of an instance of ... was altered ...
...
org.hibernate.event.def.DefaultFlushEventListener.onFlush...
...
In the beginning of investigation, we found the Hibernate related code did not flush after insert. We added the flush, but it didn't solve the problem.

Then we read Spring document and found HibernateTemplate recommended over Session. So we replaced Session with Template. But it didn't solve the problem.

Because because it had no problem going back to list after update, we tried to use saveOrUpdate and replace save. But it was the same.

Finally we noticed the Hibernate flush was scheduled after the page was forwarded, no matter what the setting of transaction.flush_before_completion was true or false. We also noticed the struts always recovered the form after forward. We tried to reset the data bean in the form right after insert. And this time, it solved the problem.

No comments: