Javers按顺序比较清单 [英] Javers Comparing List In Order

查看:443
本文介绍了Javers按顺序比较清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个像这样的java类:

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;
}

我想比较FooA,它告诉我列表对象内的哪些字段发生了变化.但是当我这样做时:

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);

这给了我这个区别:

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看起来像这样:

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'

它正在注册为值更改,而不是集合更改.我希望diff像这样阅读:

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'

我在做什么错了?

推荐答案

使用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'

几乎与您想要的相同.

It is almost the same as you wanted.

FooA/fooB/0.name-> fooA具有数组fooB,并且第一(索引0)名称属性已更改

FooA/fooB/0.name -> fooA has array fooB and first (index 0) name property has changed

这篇关于Javers按顺序比较清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆