envers-4.2.0.Final-红帽-1 .. 我的一张表有类似的实体声明:
@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;
.....
}
和其他实体:
@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;
....
}
我有一个更新的Role方法,它基本上准备了RoleDAO对象: 它所做的一件事是它创建一个新的RPMLUserDAO对象列表,如下所示:
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);
在准备好对象之后,我将使用以下代码进行更新:
em.merge(roleDAO); //EM being my entity manager object
em.flush();
执行此操作后..我在ROLE_AUD表中看到审核条目。但是如果在这个调用中我已经从userrole链接关联中删除了用户,我没有看到revtype = 2这是userrolelink_aud表中的删除..我只看到userrolelink_aud表中的添加
有人可以提供帮助..
答案 0 :(得分:1)
您需要修改现有附加角色实体上的用户列表,而不是使用新的用户列表创建新对象。
你需要做这样的事情:
roleDAO.getUsers().remove(userDAO);
而不是:
RoleDAO roleDAO = new RoleDAO();
...
roleDAO.setUsers(userDAOs);
第二种方法只会在审核表中产生添加,而第一种方法会产生您需要的删除。
这是另一个描述类似问题的问题:Envers @OneToMany audit on CREATE(0) but not on DELETE(2)