[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5127) Saving an entity in the postInsert of another entity results into SQLIntegrityConstraintViolationException

Rupesh Kumar (JIRA) noreply at atlassian.com
Fri Apr 16 07:30:58 EDT 2010


Saving an entity in the postInsert of another entity results into SQLIntegrityConstraintViolationException
----------------------------------------------------------------------------------------------------------

                 Key: HHH-5127
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5127
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
         Environment: Hibernate 3.3.2, SQLServer 
            Reporter: Rupesh Kumar
         Attachments: hibernatetest.zip

When an entity is inserted in the postInsert event of another entity, the operation fails throwing constraint violation exception for User.
I have an entity User, which I try to save in UserMgr. There is also a post Insert event Listener added to the hibernate configuration. This listener adds an audit record whenever a new entity is inserted. The id for User is assigned whereas for Audit, it is autogenerated. The weird thing is user's Id is unique and there is no other user added with that id but it always throws a constraint violation exception. 
Seeing the Hibernate source, I noticed that hibernate performs the insert batch operation and during the insert of 'User', it will call postInsert which tries to insert "Audit" which inserts in the same "insert" list. Since the id of Audit is auto-generated, the session needs to be flushed here again. At this point, hibernate tries to process the same insert list which it had processed earlier and thus throws a constraint violation exception since the user was already inserted in the first round. 
The workaround seems to be that - if there is any insert/update/delete needs to happen in the event handler, it must happen in a separate session than the one firing the event.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list