如何使用 Javers 处理嵌套实体的层次结构?

How can I handle hierarchy of nested entities with Javers?

我正在比较两个内部有嵌套集合的对象。由此产生的差异具有我所期望的一切,除了如何重建层次结构。

举个例子:

我新建了一个车库 g1,里面有两辆车 c1 和 c2。 c1有2个席位c1s1,c1s2。 c2有1个席位,c2s1。我的 SimpleTextChangeLog 看起来像这样:

new object: ...Garage/g1
new object: ...Seat/c2s1
new object: ...Car/c2
new object: ...Car/c1
new object: ...Seat/c1s1
new object: ...Seat/c1s2

我希望我的 ChangeProcessor 实现以分层形式打印更改:

new object: ...Garage/g1
    new object: ...Car/c1
        new object: ...Seat/c1s1
        new object: ...Seat/c1s2
    new object: ...Car/c2
        new object: ...Seat/c2s1

Class 层级:

@entity
class Garage {
    Set<Car> cars; 
    ...
}

@entity
class Car {
    Set<Seat> seats; 
    ...
}

@entity
class Seat {
    ...
}

有办法吗?

更改列表是一个平面的、未排序的列表,因此您无法重现层次结构形式的更改。

为什么不使用阴影?

@Entity
class Garage {
    @Id int id
    Set<Car> cars

    String toString() {
        "Garage " +id + "\n"+ cars.collect{it.toString()}
    }
}

@Entity
class Car {
    @Id int id

    String toString() {
        "Car " +id
    }
}


def "should print "(){
  when:
  def javers = JaversBuilder.javers().build()
  javers.commit("", new Garage(id:1, cars: [new Car(id:2), new Car(id:3)]))

  Shadow<Garage> g = javers.findShadows(
          QueryBuilder.byClass(Garage).withScopeCommitDeep().build())[0]

  then:
  true
  println (g.get())
}

输出:

Garage 1
[Car 2, Car 3]