Javers可以比较两个集合以为旧集合中的每个对应对象识别新集合中的对象发生的变化吗?
实现是否依赖于值对象类的equals方法?我为我的值对象实现了equals方法,但似乎仍然将它们视为不同的对象。 如果未使用equals方法,我们可以这样做吗?因为,Java Set本身依靠equals方法来确保set中不存在重复项。
请提出建议。
在下面的测试案例中,我使用sysCode和localId作为对象的组合标识符,不包括goldenId。 但是diff(该类下方的输出)无法识别相同的对象,而是将diff显示为已删除旧集合中的所有对象,并添加了新se中的对象,这似乎没有添加任何值。 / p>
这是我的测试用例:
Javers javers = JaversBuilder.javers().build();
SetConMap oldMap = new SetConMap();
Set<Contact> oldSysCons = new HashSet<>();
oldMap.setSysCons(oldSysCons);
oldSysCons.add(new Contact().sysCode("AL").localId("123").goldenId("ABC"));
oldSysCons.add(new Contact().sysCode("AL").localId("456").goldenId("ABC"));
SetConMap newMap = new SetConMap();
Set<Contact> newSysCons = new HashSet<>();
newMap.setSysCons(newSysCons);
newSysCons.add(new Contact().sysCode("AL").localId("123").goldenId("DEF"));
newSysCons.add(new Contact().sysCode("AL").localId("456").goldenId("DEF"));
newSysCons.add(new Contact().sysCode("AL").localId("789").goldenId("DEF"));
Diff diff = javers.compare(oldMap, newMap);
System.out.println(diff);
private static class Contact {
private String sysCode;
private String localId;
private String goldenId;
//getters, setters.
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj != null && (obj instanceof Contact) && Objects.equals(sysCode, ((Contact) obj).getSysCode())
&& Objects.equals(localId, ((Contact) obj).getLocalId());
}
}
差异:
* new object: org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/c76fadcd55d4dbf6d8cbe49310b1ab26
* new object: org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/96b23b34a0f42a5105491350db5c46f1
* new object: org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/a9c7f46bbe5f0e46b5751f3f5c5d8542
* object removed: org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/a53e4ba1b22c4f290a80daf40adf5d81
* object removed: org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/782edf187b8add9bab004ca06f7b6c7c
* changes on org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/ :
- 'sysCons' collection changes :
. 'org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/782edf187b8add9bab004ca06f7b6c7c' removed
. 'org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/a53e4ba1b22c4f290a80daf40adf5d81' removed
. 'org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/a9c7f46bbe5f0e46b5751f3f5c5d8542' added
. 'org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/c76fadcd55d4dbf6d8cbe49310b1ab26' added
. 'org.ishafoundation.cdi.web.rest.GoldenContactResource$SetConMap/#sysCons/96b23b34a0f42a5105491350db5c46f1' added