Javers比较不同顺序的列表

时间:2017-08-22 16:37:46

标签: java list compare javers

我有两个列表,它们具有相同的数据,但顺序不同。

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

也许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"
          }
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

首先,您必须选择一个属性作为Id属性。如果你有两个@Id属性,JaVers会选择其中一个作为Id属性(哪一个是未定义的)。

其次,用于比较列表(cannot execute nextval() in a read-only transaction LEVENSHTEIN)的两种算法都注重排序。列表是有序集合。如果您不关心订购使用套装。