如何获得嵌套的ValueObject更改?

时间:2016-04-13 06:01:32

标签: javers

我有以下类的层次结构:

class Incident {// Id => Entity
  @Id
  String id
  List<Participant> participants
  List<RealEstateProperty> realEstateProperties
}

,其中

class Participant {// No id => by javers terms - ValueObject
   EnclosedContact contact
}

class EnclosedContact {// No id => by javers terms - ValueObject
  String name
}

class RealEstateProperty {// No id => by javers terms - ValueObject
   List<CadastralSection> cadastralSections
}

class CadastralSection {// No id => by javers terms - ValueObject
  String sectionId
}

我写了以下测试(在groovy中):

def "Querying Javers Repository for participants changes works correctly"() {
    given:
    (1..3).each {
        javers.commit("author", new Incident(
            id: it,
            participants: [
                new Participant(contact: new EnclosedContact(id: 20 + it))
            ]
        ))
    }

    when:
    def snapshots = javers.findSnapshots(QueryBuilder.byValueObjectId(1, Incident.class, "contact").build())

    then:
    assert snapshots.size() == 1
}

该测试的结果是:

JaversException: PROPERTY_NOT_FOUND property 'contact' not found in class 'Incident'

尝试以这种方式进行更改

def snapshots = javers.findSnapshots(QueryBuilder.byValueObjectId(1, Incident.class, "participants/0/contact").build())

返回空列表。

Javers是否支持选择嵌套ValueObjects的更改?

3 个答案:

答案 0 :(得分:1)

在JaVers 1.6.2中,对嵌套的ValueObjects查询(尚未记录)有基本支持。您的查询应该适用于此JaVers版本持久存储的数据。例如:

def "should query for changes on nested ValueObjects stored in a list"(){
    given:
    def user = new DummyUserDetails(
        id:1,
        addressList: [new DummyAddress(networkAddress: new DummyNetworkAddress(address: "a"))])

    javers.commit("author", user)
    user.addressList[0].networkAddress.address = "b"
    javers.commit("author", user)

    when:
    def changes = javers.findChanges(QueryBuilder.byValueObjectId(1, DummyUserDetails,
            "addressList/0/networkAddress").build())

    then:
    changes.size() == 1
    changes[0].left == "a"
    changes[0].right == "b"
}

答案 1 :(得分:0)

自JaVers 2.1以来,这类查询有一个新的过滤器 - child-value-objects-filter

答案 2 :(得分:0)

我创建了一个通用的嵌套对象比较器,希望它有所帮助。

https://gist.github.com/hank-cp/3db40faed1dd9f02ababd86c2c9eaf8d

以这种方式注册:

NestedObjectComparator productRootComparator =
        new NestedObjectComparator<POProductRoot>() {};
sharedJavers = JaversBuilder.javers()
        .registerValue(POProductRoot.class)
        .registerCustomComparator(productRootComparator, POProductRoot.class).build();
productRootComparator.setJsonConverter(sharedJavers.getJsonConverter());

然后您将以MapChange格式获得更改结果。