我有两个类似的Java类:
public class FooA {
private List<FooB> fooB;
}
public class FooB {
private Integer id;
private String name;
private double num;
}
我想比较FooA,它告诉我列表对象内的哪些字段发生了变化。但是当我这样做时:
FooA old = new FooA(Arrays.asList(new FooB(1, "old", 1.0)));
FooA new = new FooA(Arrays.asList(new FooB(1, "new", 1.0)));
Javers javers = JaversBuilder.javers()
.withListCompareAlgorithm(LEVENSHTEIN_DISTANCE)
.build();
javers.compare(old, new);
这给了我这个区别:
Diff:
* new object: com.FooA/#fooB/1d32d18fcb3ba2f7f7cb41af6cd96b32
* object removed: com.FooA/#fooB/223ef3c3249fe2898ac3354f9bf42620
* changes on com.FooA/ :
- 'fooB' collection changes :
. 'com.FooA/#fooB/223ef3c3249fe2898ac3354f9bf42620' removed
. 'com.FooA/#fooB/1d32d18fcb3ba2f7f7cb41af6cd96b32' added
我什至尝试过在FooB上添加ID,就像我在很多帖子中读到的一样。所以现在我的foob看起来像这样:
public class FooB {
@Id
private Integer id;
private String name;
private double num;
}
但是现在当我比较时,我得到了:
Diff:
* changes on com.FooB/1 :
- 'name' changed from 'old' to 'new'
它正在注册为值更改,而不是集合更改。我希望diff像这样阅读:
Diff:
* changes on com.FooA/#fooB/1 :
- 'fooB' collection changes :
. 'name' changed from 'old' to 'new'
我在做什么错了?
答案 0 :(得分:1)
使用Javers
版5.2.4
:
public class FooA {
private List<FooB> fooB;
public FooA(List<FooB> fooB) {
this.fooB = fooB;
}
}
public class FooB {
private Integer id;
private String name;
private double num;
public FooB(Integer id, String name, double num) {
this.id = id;
this.name = name;
this.num = num;
}
}
void test() {
FooA old = new FooA(Arrays.asList(new FooB(1, "old", 1.0)));
FooA new1 = new FooA(Arrays.asList(new FooB(1, "new", 1.0)));
Javers javers = JaversBuilder.javers()
.withListCompareAlgorithm(LEVENSHTEIN_DISTANCE)
.build();
System.out.println(
javers.compare(old, new1)
);
}
方法test
返回:
Diff:
* changes on pl.javers.JaversTest$FooA/ :
- 'fooB/0.name' changed from 'old' to 'new'
几乎与您想要的相同。
FooA/fooB/0.name
-> fooA具有数组fooB,并且第一个(索引0)name属性已更改