Question

envers-4.2.0.Final-redhat-1.. One of my table has similar looking entity declaration:

@Entity(name="ROLE")
@Audited
public class RoleDAO {

    @Id
    @TableGenerator(name="ROLE_GEN", 
                    table="RPML_ID_GEN", 
                    pkColumnName="GEN_KEY",
                    valueColumnName="GEN_VALUE", 
                    pkColumnValue="ROLEID",
                    allocationSize=1)
    @GeneratedValue(strategy=GenerationType.TABLE, generator="ROLE_GEN")
    @Column(name = "ROLEID", nullable = false)
    private int roleId;

    @Column(name = "NAME", length=50, unique=true)
    private String name;

    @Column(name = "ENABLED")
    private boolean enabled;

    @ManyToMany(fetch = FetchType.LAZY) 
    @JoinTable(name="RPMLUSERROLELINK",
            joinColumns={@JoinColumn(name="ROLEFKID")},
            inverseJoinColumns={@JoinColumn(name="USERFKID")})  
    private List<RPMLUserDAO> users;

.....
}

and other Entity:

@Entity(name="RPMLUSER")
@Audited
public class RPMLUserDAO {

    @Id
    @TableGenerator(name="USER_GEN", 
                    table="RPML_ID_GEN", 
                    pkColumnName="GEN_KEY",
                    valueColumnName="GEN_VALUE", 
                    pkColumnValue="USERID",
                    allocationSize=1)
    @GeneratedValue(strategy=GenerationType.TABLE, generator="USER_GEN")
    @Column(name = "USERID", nullable = false)
    private int userId;

    @Column(name = "LOGIN", length=50, unique=true, nullable = false)
    private String login;

    @Column(name = "EMAIL", length=70, unique=true, nullable = false)
    private String email;

    @Column(name = "FIRSTNAME", length=50, nullable = false)
    private String firstName;

    @Column(name = "LASTNAME", length=50, nullable = false)
    private String lastName;

    @Column(name = "ENABLED")
    private boolean enabled;

    @Column(name= "LASTUPDATEDBY")
    private String lastUpdatedBy;

    @ManyToMany( fetch = FetchType.LAZY)
    @JoinTable(name="RPMLUSERROLELINK",
            joinColumns={@JoinColumn(name="USERFKID")},
            inverseJoinColumns={@JoinColumn(name="ROLEFKID")})
    private List<RoleDAO> roles;
....
}

I have an update Role method which essentially prepares the RoleDAO object : One of the thing it does is it creates a new list of RPMLUserDAO objects as follows:

List<RPMLUserDAO> userDAOs = new ArrayList<RPMLUserDAO>();

for (DeploymentUser user : users) {
    RPMLUserDAO userDAO = adminDal.findUser(user.getId()); // call to the DAL layer which does a select and returns the userDAO object 
    userDAO.setLastUpdatedBy(lastUpdatedBy);
    userDAOs.add(userDAO);
}

roleDAO.setUsers(userDAOs);

After the object is prepared I udpate it using following piece of code :

em.merge(roleDAO); //EM being my entity manager object
em.flush();

After this is excuted .. I see audit entries in ROLE_AUD table . But if in this call i have removed the user from userrole link association I dont see the revtype = 2 which is a delete in the userrolelink_aud table .. I only see adds in the userrolelink_aud table

Can someone help ..

Was it helpful?

Solution

You need to modify the list of users on an existing attached role entity rather than creating a new object with a new list of users.

You need to do something like this:

roleDAO.getUsers().remove(userDAO);

rather than:

RoleDAO roleDAO = new RoleDAO();
...
roleDAO.setUsers(userDAOs);

The second approach will only produce adds in the audit table whereas the first one will produce the deletes you require.

Here's another question which describes a similar problem: Envers @OneToMany audit on CREATE(0) but not on DELETE(2)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top