Envers store_data_at_delete does not populate entity id on delete revision

Description

First posted from the Hibernate Envers community forum:
https://community.jboss.org/message/823516#823516

Hibernate Envers setup with 'store_data_at_delete' = 'true' in order to provide an additional revision when an entity is deleted. In Hibernate 4.2.1.Final, when a mapping entity is deleted, Hibernate Envers attempts to insert a deletion revision record into the appropriate audit table, but does not populate the insert statement with the id of the entity that is being deleted.

The delete revision record was not being inserted at all in Hibernate 4.1.12.Final, but after viewing HHH-8087 and upgrading, this issue was observed.

The use case I had was the following with all entities having an @Audited annotation.

  • User table - User metadata

  • User group table - User group metadata. Deleting a user group deletes all user group member associations via cascade.

  • User group member table - A mapping table to associate users to one or more user groups.

1. Create user
2. Create user group
3. Add user to user group (Envers inserts an 'ADD (0)' revision for user group member.)
4. Delete user group which also cascades to delete associated user group member records (Envers inserts a 'DEL (2)' revision for user group member with no user group member id.

The user group member audit table schema is below...

Table "user_group_member_aud" Column | Type | Modifiers ---------------+----------+----------- rev | integer | not null user_group_id | uuid | not null user_id | uuid | not null revtype | smallint | id | uuid | not null Indexes: "user_group_member_aud_pkey" PRIMARY KEY, btree (id, rev)

The hibernate log shows as follows as the insert is done with no id. The db constraint prevents the insertion due to the not null constraint on the audit table id column.

Hibernate: insert into user_group_member_AUD (REVTYPE, REV, user_group_id, user_id) values (?, ?, ?, ?) 2013-06-14 09:57:10,430 WARN [SqlExceptionHelper] - SQL Error: 0, SQLState: 23502 2013-06-14 09:57:10,430 ERROR [SqlExceptionHelper] - Batch entry 0 insert into user_group_member_AUD (REVTYPE, REV, user_group_id, user_id) values ('2', '4900', 'b15f1cee-1eea-4a51-a9df-4fb90f8eb95c', '361d0717-7d8a-4af9-9b17-871190052d1c') was aborted. Call getNextException to see the cause. 2013-06-14 09:57:10,431 WARN [SqlExceptionHelper] - SQL Error: 0, SQLState: 23502 2013-06-14 09:57:10,431 ERROR [SqlExceptionHelper] - ERROR: null value in column "id" violates not-null constraint

Attachments

1
  • 30 Jul 2013, 08:04 PM

Activity

Lukasz AntoniakJuly 31, 2013 at 10:06 AM

Data model in attached test case seems invalid. Both, entity UserGroupMember and property UserGroup#userGroupMembers, map to the same table user_group_member. Just rename one of them and you will receive a proper integrity constraint violation error. There is no cascading in this example.

Seth KatzmanJuly 30, 2013 at 8:04 PM

I was able to create a standalone application which demonstrates the root issue(s). Attached usermgmt.test.sources.jar.

The notable class to examine/execute is hhh8320.CascadeTest. The hhh8320.entity package contains the annotated entities: User, UserGroup, UserGroupMember, and AuditRevision (revinfo).

CascadeTest shows two issues which may be related.

CascadeTest#testCascadeDeleteUserGroupMemberOnUserGroupDeletion()

1. Create user, user group, and user group member associating a user to the user group.
2. Then delete user group which cascade deletes the associated user group member. Envers attempts to insert a DELETE revision in the user group member audit table without defining the required entity id field. A constraint violation occurs.

CascadeTest#testCascadeDeleteUserGroupMemberOnUserDeletion()

1. Create user, user group, and user group member associating a user to the user group.
2. Then delete user which cascade deletes the associated user group member. No error occurs. Envers inserts a DELETE revision for the user audit table which is expected. However, Envers does not insert a DELETE revision for the cascade deleted user group member table.

Seth KatzmanJuly 25, 2013 at 3:20 PM

I'll see what I can put together for a test to reproduce the given issue. I'll post back to this thread once I have additional information.

Lukasz AntoniakJuly 24, 2013 at 4:26 PM

Could you provide complete mapping of your entity classes, including custom type @Type(type = CUSTOM_UUID_TYPE) mentioned in the forum thread? How do you cascade removal of associated entities? What is the sequence of performed Session or EntityManager operations? I would appreciate small application containing one unit test reproducing the issue.

Rejected

Details

Assignee

Reporter

Labels

Affects versions

Priority

Created June 19, 2013 at 12:49 PM
Updated December 19, 2013 at 3:55 PM
Resolved December 19, 2013 at 3:55 PM

Flag notifications