比较 child 个实体列表时获取 parent 个实体 ID

Get parent entity ID when comparing list of child entities

Javers v5.6.3

我有一个 parent 实体,其中包含 child 个实体的列表。当我比较 2 个 parent 列表并在 child 中发现更改时,我需要知道哪个 parent 包含更改的 child.

我的实体:

@TypeName("Parent")
class Parent {
    @Id
    private String parentId;
    private Set<Child> children;
}

@TypeName("Child")
class Child {
    @Id
    private String childId;
    private String someProp;
}

我要区分的是:

为简洁起见切换到 Groovy:

def list1 = [
      new Parent(parentId:'p1', children: [new Child(childId: 'c1')]),
      new Parent(parentId:'p2', children: [new Child(childId: 'c2', someProp: 'fig')])
]
def list2 = [
      new Parent(parentId:'p1', children: [new Child(childId: 'c1', someProp: 'apple')]),
      new Parent(parentId:'p2', children: [new Child(childId: 'c2', someProp: 'fig')])
]

我的差异:

Javers javers = JaversBuilder.javers()
                  .withListCompareAlgorithm(ListCompareAlgorithm.AS_SET)
                  .build();

Diff diff = javers.compareCollections(list1, list2);
System.out.println(diff);

输出:

Diff:
* changes on Child/c1 :
  - 'someProp' value changed from '' to 'apple'

我的问题:

如何发现哪个 parent 包含 Child/c1?

这与 shouldDetectSalaryChange() example 类似,只是我有一组 Boss。使用该示例,给定一份拥有不同下属的老板列表,如何找出 Great Developer 的老板?

我是否需要创建自定义 Parent 比较器,或者有没有办法从 built-in 比较器中找到答案?

有了这个映射,您就无法发现哪个 Parent 包含 Child/c1,因为 Child class 被映射为 Entity。所以Javers并不知道这段亲子关系

您可以更改 Child class 到 ValueObject 的映射以使此关系明确:

    @TypeName("Parent")
    class Parent {
        @Id
        private String parentId
        private Set<Child> children
    }

    @TypeName("Child")
    class Child {
        private String childId
        private String someProp
    }

然后:

        Javers javers = JaversBuilder.javers()
                .withListCompareAlgorithm(ListCompareAlgorithm.AS_SET)
                .wi
                .build()

        Diff diff = javers.compareCollections(list1, list2, Parent)

        println(diff)

打印:

Diff:
* new object: Parent/p1#children/5f6ade8312fb0c5d60f756841e23af65
* object removed: Parent/p1#children/9de1d946eab394f971263c33e297f86d
* changes on Parent/p1 :
  - 'children' collection changes :
    . 'Parent/p1#children/9de1d946eab394f971263c33e297f86d' removed
    . 'Parent/p1#children/5f6ade8312fb0c5d60f756841e23af65' added