我有两个数据相同但顺序不同的列表。
为什么JaverscompareCollections
方法看到Levenshtein距离的两个变化(一个ValueRemoved和一个ValueAdd(?
也许Javers不允许带有注释@Id
多个字段?
法典:
class A {
@Id
private String a;
@Id
private String b;
public A() {
}
public A(String a, String b) {
this.a = a;
this.b = b;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
@Test
public void testJavers() {
List<A> oldList = Arrays.asList(
new A("a1", "b1"),
new A("a2", "b2"));
List<A> newList = Arrays.asList(
new A("a2", "b2"),
new A("a1", "b1"));
Javers javers = JaversBuilder.javers()
.withListCompareAlgorithm(ListCompareAlgorithm.LEVENSHTEIN_DISTANCE)
.build();
Diff diff = javers.compareCollections(oldList, newList, A.class);
System.out.println(javers.getJsonConverter().toJson(diff));
}
结果:
{
"changes": [
{
"changeType": "ListChange",
"globalId": {
"valueObject": "org.javers.core.graph.LiveGraphFactory$ListWrapper"
},
"property": "list",
"elementChanges": [
{
"elementChangeType": "ValueRemoved",
"index": 1,
"value": {
"a": "a2",
"b": "b2"
}
},
{
"elementChangeType": "ValueAdded",
"index": 0,
"value": {
"a": "a2",
"b": "b2"
}
}
]
}
]
}
首先,您必须只选择一个属性作为 Id 属性。如果有两个属性具有@Id,JaVers 会选择其中一个作为 Id 属性(哪个属性未定义(。
其次,比较列表的两种算法(LEVENSHTEIN
和SIMPLE
(都注重排序。列表是有序集合。如果您不关心订购,请使用 Sets。