moisko
moisko

Reputation: 101

How to get nested ValueObject changes?

I have the following hierarchy of classes:

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

where

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
}

I have written the following test (in 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
}

The result of this test is:

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

Trying to get the changes this way

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

returns empty list.

Does Javers support selecting for changes on nested ValueObjects?

Upvotes: 1

Views: 2878

Answers (3)

Hank
Hank

Reputation: 1338

I created a generic nested object comparator, hope it helps.

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

Registers it like this way:

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

Then your will get the changes result in MapChange format.

Upvotes: 1

Bartek Walacik
Bartek Walacik

Reputation: 3496

Since JaVers 2.1, there is a new filter for this type of queries - child-value-objects-filter

Upvotes: 0

Bartek Walacik
Bartek Walacik

Reputation: 3496

in JaVers 1.6.2 there is a basic support for nested ValueObjects queries (undocumented yet). Your query should work for data persisted by this JaVers version. For example:

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

Upvotes: 2

Related Questions