Javers 比较不同顺序的列表

Javers comparing lists with different order

我有两个包含相同数据但顺序不同的列表。

为什么 Javers compareCollections 方法看到两个变化(一个 ValueRemoved 和一个 ValueAdded)与 Levenshtein 距离?

也许 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-属性(其中一个未定义)。

其次,比较列表的两种算法(LEVENSHTEINSIMPLE)都注意排序。列表是有序的集合。如果您不关心订购,请使用套装。