Javers将添加的对象检索到列表

时间:2019-02-14 14:38:05

标签: java javers

在Javers中,是否可以检索列表(或任何Container)的添加对象?

我不想要GlobalId,而是真实对象实例。

getAddedValues似乎返回一个GlobalId而不是实际对象。

注意:我事先不知道要比较的对象类型。这是一个示例对象。

MyTestObject obj1 = //Initialise it
MyTestObject obj2 = //Initialise it

Diff diff = javers.compare(obj1, obj2);

List<ContainerChange> containerChanges = diff.getChangesByType(ContainerChange.class);

for (ContainerChange containerChange : containerChanges) {
    for (Object addedValue : containerChange.getAddedValues()) {
        containerChange.getAffectedObject() //Returns the affected object
        addedValue //Returns an GlobalId but I want the real object added. (in this case the subObject added) Like the getAffectedObject() but with the other object.
    }
}

@TypeName("MyTestObject")
private static class MyTestObject {
    @Id
    private int id;
    private String value;
    private List<SubObject> subObjects;

    private MyTestObject() {

    }

    private MyTestObject(int id, String value, List<SubObject> subObjects) {
        this.id = id;
        this.value = value;
        this.subObjects = subObjects;
    }
}

@TypeName("SubObject")
private static class SubObject {
    @Id
    private int id;
    private String value;

    private SubObject() {

    }

    private SubObject(int id, String value) {
        this.id = id;
        this.value = value;
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了其他一些changeType的解决方案。我能够使用NewObject和ObjectRemoved的getChangesByType来构建地图。在这些更改中,对象存在于受影响的对象中。

类似这样的东西:

     map = Stream.of(NewObject.class, ObjectRemoved.class)
                .flatMap(type -> diff.getChangesByType(type).stream())
                .collect(
                        Collectors.toMap(change -> change.getAffectedGlobalId().toString(), 
                                         change -> change.getAffectedObject().get()));

并检索带有addedValue的对象

addedAndRemovedObjects.get(containerChange.getAddedValues().get(1).toString());

注1:我可能会更改toString键的其他内容,但它适用于我的测试示例。有一个GlobalId和IdentityId不等于,但是toString是。

注2:getAddedValues()。get(1)仅用于示例。