Javers 能否比较两个包含 valueObjects 的 Sets,用 equals/hashcode 方法来识别它们以检测变化?
Can Javers compare two Sets containing valueObjects identifying them with the equals/hashcode methods to detect changes?
Javers 能否通过比较两个集合来识别旧集合中每个对应对象在新集合中的对象发生的变化?
实现是否依赖于值对象的equals方法class?我为我的值对象实现了 equals 方法,但它似乎仍然将它们视为不同的对象。
如果不使用equals方法,我们可以这样做吗?因为,Java Set 本身依赖于 equals 方法来确保集合中不存在重复项。
求推荐。
在下面的测试用例中,我使用 sysCode 和 localId 作为对象的组合标识符,不包括 goldenId。
但是差异(class 下方的输出)没有识别出相等的对象并将差异显示为旧集合中的所有对象都被删除而新 se 中的对象被添加,这似乎没有添加任意值。
这是我的测试用例:
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
Javers 能否通过比较两个集合来识别旧集合中每个对应对象在新集合中的对象发生的变化?
实现是否依赖于值对象的equals方法class?我为我的值对象实现了 equals 方法,但它似乎仍然将它们视为不同的对象。 如果不使用equals方法,我们可以这样做吗?因为,Java Set 本身依赖于 equals 方法来确保集合中不存在重复项。
求推荐。
在下面的测试用例中,我使用 sysCode 和 localId 作为对象的组合标识符,不包括 goldenId。 但是差异(class 下方的输出)没有识别出相等的对象并将差异显示为旧集合中的所有对象都被删除而新 se 中的对象被添加,这似乎没有添加任意值。
这是我的测试用例:
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