Javers对集合成员属性的更改显示为新对象

时间:2018-07-23 12:46:58

标签: java collections javers

以下测试失败

  @Test
  public void test() {
    Javers javers = javers().build();
    Model anObject = new Model();
    Model anotherObject = new Model();

    Aggregate container = new AggregateHashSet();
    container.getInnerModels().add(anObject);
    container.getInnerModels().add(anotherObject);

    Commit firstCommit = javers.commit("author", container);
    log.info(firstCommit);

    anObject.setProperty("bob");
    anotherObject.setProperty("bob");

    Commit secondCommit = javers.commit("anotherAuthor", container);
    log.info(secondCommit);

    List<Change> changes = secondCommit.getChanges();
    List<Object> afftectedObjects = 

   changes.stream()
     .map(Change::getAffectedObject)
     .map(Optional::get).collect(toList());
   assertThat(afftectedObjects, containsInAnyOrder(anObject, anotherObject));
  }

用作模型和容器的类没什么特别的

  static class Model {

     private String property;


     public String getProperty() {
       return property;
     }


     public void setProperty(String property) {
       this.property = property;
     }
   }

  interface AggregateSet extends Aggregate {
    @Override
    Set<Model> getInnerModels();
  }

  static class AggregateHashSet implements AggregateSet {
    private final Set<Model> innerModels = newHashSet();

   @Override
   public Set<Model> getInnerModels() {
     return innerModels;
   }
  }

失败是

java.lang.AssertionError: 
Expected: iterable over [<TestTracker$Model@4df50bcc>, <TestTracker$Model@6b26e945>] in any order
     but: Not matched: <TestTracker$AggregateHashSet@4690b489>

我从第二次提交获得的更改是

[NewObject{ new object: TestTracker$AggregateHashSet/#innerModels/86f2152a7afb26768aa24750735c8e4c }, NewObject{ new object: TestTracker$AggregateHashSet/#innerModels/b3d3dfd282c37280661e3e5963cac4ba }, SetChange{ 'innerModels' collection changes :
  . 'TestTracker$AggregateHashSet/#innerModels/446f556711adf764944f1f7e10601cff' removed
  . 'TestTracker$AggregateHashSet/#innerModels/86f2152a7afb26768aa24750735c8e4c' added
  . 'TestTracker$AggregateHashSet/#innerModels/b3d3dfd282c37280661e3e5963cac4ba' added }]
[NewObject{ new object: TestTracker$AggregateHashSet/#innerModels/86f2152a7afb26768aa24750735c8e4c }, NewObject{ new object: TestTracker$AggregateHashSet/#innerModels/b3d3dfd282c37280661e3e5963cac4ba }, SetChange{ 'innerModels' collection changes :
  . 'TestTracker$AggregateHashSet/#innerModels/446f556711adf764944f1f7e10601cff' removed
  . 'TestTracker$AggregateHashSet/#innerModels/86f2152a7afb26768aa24750735c8e4c' added
  . 'TestTracker$AggregateHashSet/#innerModels/b3d3dfd282c37280661e3e5963cac4ba' added }]
[NewObject{ new object: TestTracker$AggregateHashSet/#innerModels/86f2152a7afb26768aa24750735c8e4c }, NewObject{ new object: TestTracker$AggregateHashSet/#innerModels/b3d3dfd282c37280661e3e5963cac4ba }, SetChange{ 'innerModels' collection changes :
  . 'TestTracker$AggregateHashSet/#innerModels/446f556711adf764944f1f7e10601cff' removed
  . 'TestTracker$AggregateHashSet/#innerModels/86f2152a7afb26768aa24750735c8e4c' added
  . 'TestTracker$AggregateHashSet/#innerModels/b3d3dfd282c37280661e3e5963cac4ba' added }]

但是,在第一次提交和第二次提交之间,没有从集合中添加/删除新对象

如果我将AggregateHashSet替换为AggregateModelArrayList

  static class AggregateModelArrayList implements AggregateList {

    private final List<Model> innerModels = newArrayList();


    @Override
    public List<Model> getInnerModels() {
      return innerModels;
    }

  }

测试通过。报告的更改是(正如我所期望的那样)

[ValueChange{ 'innerModels/0.property' changed from '' to 'bob' }, ValueChange{ 'innerModels/1.property' changed from '' to 'bab' }]

我想念什么?

E2A:使用Javers 3.10.2

1 个答案:

答案 0 :(得分:0)

将ValueObjects存储在Set中时,JaVers会为其生成标识符。这些标识符基于toString的哈希值。因此,如果更改对象的属性,它将获得新的标识符。您可以在更改打印中看到这些标识符。列表的工作方式不同,标识符基于列表索引。我的建议是,如果要比较Set,请尝试将项目类映射为Value或Entity