1

We are using hibernate envers to audit changes in entities.
We have following two entities
UserRole

public class UserRole {
    private String id;

    @Audited
    @Column(name = "name")
    private String name;

    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToMany(mappedBy = "userRoles", cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
    })
    @Audited
    private Set<UserGroup> userGroups;
}


UserGroup

public class UserGroup {
    private String id;

    @Audited
    @Column(name = "name")
    private String name;

    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToMany(cascade = {
        CascadeType.PERSIST,
        CascadeType.MERGE
    })
    @JoinTable(
        name = "role_user_group_mapping",
        joinColumns = {@JoinColumn(name = "user_group_id")},
        inverseJoinColumns = {@JoinColumn(name = "user_role_id")}
    )
    @Audited
    private Set<UserRole> userRoles;

}


Following is the scenario:
if we try to assign role to any of the existing user group, envers will record all other roles which are already assigned to the usergroup as modified, even if there were no changes in those roles.


eg. suppose we have user group "Admin User Group"
and it has "Role1" and "Role2" already assigned to it.
Now if I try to assign new role "Role3" to this group, I will get extra audits for the existing roles "Role1" and "Role2" as modified but there were no changes in these roles.

Audit should be only for the user group "Admin User Group" since new role was assigned to it and for the "Role3" since new user group was assigned to this role.

How to avoid already existing roles ("Role1", "Role2") getting audited and only audit actually modified entities?

Sandesh
  • 428
  • 9
  • 22
  • you can try `@AuditJoinTable`. 1) https://stackoverflow.com/questions/15030588/audit-manytomany-relationships-using-hibernate-envers 2) https://stackoverflow.com/questions/50927066/hibernate-whats-the-difference-between-audittable-and-auditjointable/50928235. – Dmitrii Bocharov Apr 01 '20 at 14:55
  • @bdshadow still facing the same issue. @ AuditJoinTable added on userGroups in UserRole entity and userRoles in UserGroup entity. – Sandesh Apr 01 '20 at 15:39
  • If I remove @ Audited from userRoles in UserGroup entity and just keep AuditJoinTable, it will not even audit the changes made by adding new role to usergroup. – Sandesh Apr 01 '20 at 17:38
  • 1
    It sounds to me like you have something else going on or the mappings you've illustrated don't exactly mirror your real use case. In a transaction I fetched the admin group, persisted a new role 3 and associated this role 3 to the group and there were 4 audit rows inserted, one into `REVINFO`, one into `UserRole_AUD`, one into `role_user_group_mapping_AUD` and one into `UserGroup_AUD`. Can you update your question with the code on how you're creating and persisting Role3 and merging the changes to your admin group? – Naros Apr 01 '20 at 18:34
  • @Naros You are right. Problem was in our code logic. We were trying to set reference of the usergroup in all the existing and new roles instead of only in new roles. Thanks for the help. – Sandesh Apr 03 '20 at 06:04

0 Answers0