至于envers而言,基本用例是记录一个实体的完整的审计(参数我们希望通过@Audited注释)。对于您提到的情况,可能会正确添加新实体,但对于现有的实体可能会带来问题,因为审计表中没有修订。
让我们破案有场景的帮助:
比方说,我们已经考虑到实体用户。现在创建的表格用于观察历史记录,比方说,users_audit。除此之外,revinfo也将适用于观察和记录对于给定记录的所有变化。
问题首先出现,因为每当有更新时,持久层无法找到修订记录。因此,为了解决这个问题,需要在表中存在所有现有的条目,并且不应该使用revinfo表进行外键映射。因此,需要两件事情要做:
- 插入Temp值在revinfo表,以便转可以作为外键
- 复制从用户行为数据表到users_audit表。
样品Liquibase文件可以是这样的:
CREATE TABLE `revinfo` (
`rev` int(11) NOT NULL AUTO_INCREMENT,
`revtstmp` bigint(20) DEFAULT NULL,
PRIMARY KEY (`rev`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO `revinfo` (`revtstmp`) select updated_at from users u;
SET @position := 0;
insert into users_audit (
rev,
revtype,
id,
name,
type,
mobile_number,
password,
parent_id,
profile_image_uri,
is_active,
created_at,
updated_at
) select @position := @position +1, 0,
id,
name,
type,
mobile_number,
password,
parent_id,
profile_image_uri,
is_active,
created_at,
updated_at from us
嗨!你解决了你的问题吗?我也有同样的问题。 – gipinani 2013-08-30 05:22:58
不,我给Envers并使用ON INSERT和ON UPDATE数据库触发器 – 2013-08-30 13:42:53