1

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

为什么 JaverscompareCollections方法会看到 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"
          }
        }
      ]
    }
  ]
}
4

1 回答 1

0

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

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

于 2017-08-23T13:24:31.057 回答