我想通过使用Spring data Rest的历史表来跟踪我的数据的变化。我的预期行为是,对于每个更改数据的事务,都会自动将一个条目添加到我的历史表中。我不确定,但我不认为在使用服务时拥有跨越多个请求的事务是有效的。
我目前有两个选择:
1)对数据使用数据库触发器--我在这里看到的缺点是,目前我们的大部分操作都是通过Java完成的,我看不到任何通过Java来实现这一点的方法。
2)使用带注释的事件处理程序-我们会为每个表的每个事件创建事件处理程序,以跟踪历史,我倾向于这样做,但我不确定这是不是正确的方式。
这两个选项中哪一个更好?还有其他可用的选项吗?
发布于 2014-09-18 04:21:38
使用Spring Data Rest,您可以选择使用events执行任何预处理和后处理。我相信原始事务边界适用于这些事件中的任何数据库操作。
发布于 2017-12-11 15:21:59
使用Envers official site进行操作审计
发布于 2017-12-11 03:40:03
我知道几年后我会回答这个问题,但这是另一种方法:
创建RepositoryEventHandler
@Component
@RepositoryEventHandler(Code.class)
public class CodesEventHandler {
@Autowired
private CodesRepository repository;
@HandleBeforeSave
public void handleBeforeSave(Code c) {
// backup the existing data to history (happens to be on the same table)
Code newC = new Code();
BeanUtils.copyProperties(c, newC);
CodeId newId = new CodeId();
BeanUtils.copyProperties(c.getCodeId(), newId);
newC.setCodeId(newId);
Date now = new Date();
newC.getCodeId().setValidToTs(now);
repository.save(newC);
c.setValidFromTs(now);
}
@HandleBeforeDelete
public void handleBeforeDelete(Code c) {
// same as above but this time set the end timestamp to be now
}
}https://stackoverflow.com/questions/25883343
复制相似问题