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?