我创建了一个CustomPropertyComparator来比较jaxb在哪里创建的对象。因此,就我而言,我无法使用框架提供的 @DiffIgnore 批注。 jaxb生成的Collections也基于java.util.List而不是java.util.Collection。不幸的是,我无法让javers使用带有List-Interface的CustomPropertyComparator。
public class Person {
private String name;
private String ignoreThis;
}
public class Company {
private String id;
private Person owner;
private Collection<Person> clients;
private List<Person> partners;
}
仅比较名称,但忽略字段“ ignoreThis”的比较器
public class EntityComparator implements CustomPropertyComparator<Person, ValueChange> {
public ValueChange compare(Person left, Person right, GlobalId affectedId, Property property) {
if (left.getName().equals(right.getName()))
return null;
return new ValueChange(affectedId, "entity/name", left.getName(), right.getName());
}
}
我的测试用例如下:
此测试有效,因为它比较了集合
@Test
public void equalEntityClientTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", null, Arrays.asList(e1), null);
Person e2 = new Person("james", "");
Company le2 = new Company("1", null, Arrays.asList(e2), null);
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
此测试失败,因为它没有使用我的比较器比较实体,并且忽略字段的差异为true。
@Test
public void equalEntityPartnerTest() {
Person e1 = new Person("james", "ignore this");
Company le1 = new Company("1", e1, null, Arrays.asList(e1));
Person e2 = new Person("james", "");
Company le2 = new Company("1", e2, null, Arrays.asList(e2));
Diff diff = javers.compare(le1, le2);
System.out.println(diff);
assertEquals(0, diff.getChanges().size());
}
在javers的参考中,他们解释了如果您有一个自定义的Collection-Interface,则需要实现自己的比较器,如果您使用的不是基于java.util.Collection的collection,那也可以。但是实际上我希望javers库支持java.util.List。 另外,我也无法弄清楚如何为列表接口添加/创建比较器。
下找到答案 0 :(得分:1)
您在这里碰到了两个问题。
您的第一个测试通过了,因为Javers忽略了Collection
属性,并且仅记录了以下警告:
10:19:40.332 [main] WARN o.j.c.d.a.CollectionChangeFakeAppender - Collections: Field Collection<Person> clients; //declared in Company
are not equals but can't be compared. Raw Collection properties are not supported. Expected Set, List or any of their subclasses. JaVers uses different algorithms for comparing Sets and Lists and needs to know (statically) which one to test.
我认为这不行,所以我创建了一个问题,请参见https://github.com/javers/javers/issues/746
第二个问题涉及CustomPropertyComparator
。它旨在比较大型结构(例如Multimap),并且在比较列表项时JaVers不会调用它,因为在比较列表项时,您需要boolean equals(a,b)
方法。这就是CustomValueComparator
的作用。
尽管可以将CustomPropertyComparator扩展为也具有boolean equals(a,b)
方法。我已经为此创建了第二个问题,请参见https://github.com/javers/javers/issues/747