问题
I have two java classes like so:
public class FooA {
private List<FooB> fooB;
}
public class FooB {
private Integer id;
private String name;
private double num;
}
I want to compare the FooA and it tell me what fields inside the list object changed. But when I do this:
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);
It gives me this diff:
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
I even tried adding an Id on FooB like I've read in a lot of posts. So now my foob looks like this:
public class FooB {
@Id
private Integer id;
private String name;
private double num;
}
But now when I compare I get this:
Diff:
* changes on com.FooB/1 :
- 'name' changed from 'old' to 'new'
It's registering as a value change rather than a collection change. I want the diff to read like so:
Diff:
* changes on com.FooA/#fooB/1 :
- 'fooB' collection changes :
. 'name' changed from 'old' to 'new'
What am I doing wrong?
回答1:
Using Javers
version 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)
);
}
method test
returns:
Diff:
* changes on pl.javers.JaversTest$FooA/ :
- 'fooB/0.name' changed from 'old' to 'new'
It is almost the same as you wanted.
FooA/fooB/0.name
-> fooA has array fooB and first (index 0) name property has changed
来源:https://stackoverflow.com/questions/55031433/javers-comparing-list-in-order