是否可以使用JaVers记录Map中值的更改?
我想将所有更改(添加,更新和删除)记录到我的RecordStore对象中,以便我对已发生的事情进行审计跟踪。目前,我正在使用以下代码,并在每次更改后提交。
private Map<String, Record> records = new HashMap<>();
private Javers history = JaversBuilder.javers().build();
public RecordStore(){
history.commit("user", records);
}
public void addRecord(Record a){
records.put(a.getId(), a);
history.commit("user", records);
}
public void removeRecord(Record a){
records.remove(a.getId());
history.commit("user", records);
}
public void updateRecord(Record a){
records.put(a.getId(), a);
history.commit("user", records);
}
public String printHistory(){
List<Change> changes = history.findChanges(QueryBuilder.anyDomainObject().build());
return history.processChangeList(changes, new SimpleTextChangeLog());
}
但是,JaVers(printHistory()
)生成的更改列表中只包含EntryAdded和EntryRemoved记录。更糟糕的是,它在整个日志中使用更改的值,而不是仅在更改后使用。我之前没有使用过JaVers,所以任何帮助它按照需要运行都会受到赞赏。使用RecordStore的测试类如下:
public static void main(String[] args){
RecordStore store = new RecordStore();
Record a1 = new Record();
a1.setStart(0);
a1.setEnd(5);
a1.setValue("James");
store.addRecord(a1);
Record a2 = new Record();
a2.setStart(10);
a2.setEnd(15);
a2.setValue("Jimmy");
store.addRecord(a2);
Record a3 = new Record();
a3.setStart(20);
a3.setEnd(25);
a3.setValue("Jimbo");
store.addRecord(a3);
a2.setValue("JIMMY");
store.updateRecord(a2);
store.removeRecord(a1);
System.out.println(store.printHistory());
}
答案 0 :(得分:0)
通常,您逐个提交域对象,例如:
public void addRecord(Record a){
records.put(a.getId(), a);
history.commit("user", a);
}
public void removeRecord(Record a){
records.remove(a.getId());
history.commitShallowDelete("user", a);
}
public void updateRecord(Record a){
records.put(a.getId(), a);
history.commit("user", a);
}
但是,Record应该映射为实体类型(使用@Id)。