2017-06-06 73 views
0

我想使用JaVers来跟踪我的Java对象的更改。基本的例子工作正常,但我无法检测到存储在集合中的对象的更改。JaVers未检测到集合中对象属性的更改

如果我扩展ChangeLogExample.class示例以更改例如子坐标之一:

public static void main(String[] args) { 

    Javers javers = JaversBuilder.javers().build(); 
    Employee bob = new Employee("Bob", 9_000, "Scrum master"); 
    javers.commit("hr.manager", bob); 

    // do some changes and commit 
    bob.setPosition("Team Lead"); 
    bob.setSalary(11_000); 
    javers.commit("hr.director", bob); 

    bob.addSubordinates(new Employee("Trainee One"), new Employee("Trainee Two")); 
    javers.commit("hr.manager", bob); 

    bob.getSubordinates().get(0).setAge(42); // <<<< This is the change I want to detect 
    javers.commit("hr.manager", bob); 

    // when: 
    List<Change> changes = javers.findChanges(
     QueryBuilder.byInstanceId("Bob", Employee.class).withChildValueObjects().build()); 
    String changeLog = javers.processChangeList(changes, new SimpleTextChangeLog()); 

    // then: 
    System.out.println(changeLog); 
} 

这是印在更新日志:

commit 3.0, author: hr.manager, 2017-06-06 11:17:17 
    changed object: Employee/Bob 
    list changed on 'subordinates' property: [(0).added:'Employee/Trainee One', (1).added:'Employee/Trainee Two'] 
commit 2.0, author: hr.director, 2017-06-06 11:17:17 
    changed object: Employee/Bob 
    value changed on 'position' property: 'Scrum master' -> 'Team Lead' 
    value changed on 'salary' property: '9000' -> '11000' 

所以换到第一下属的年龄不会在更新日志显示。使用withChildValueObjects()不会产生影响。

当我将更改单独提交给Employee实例时,我得到了实习生年龄的变化,但这不是我想要的(也不是我想要的)。

所以我的问题是:如何让更改显示在ChangeLog中?


我使用JaVers 3.2.0

Employee类是从JaVers例子不变:https://github.com/javers/javers/tree/master/javers-core/src/test/java/org/javers/core/examples/model

main()方法是简单地从https://github.com/javers/javers/blob/master/javers-core/src/test/java/org/javers/core/examples/ChangeLogExample.java

回答

0

好,几个测试问题在这里。 首先,Empolyee对象映射为Entities。所以在JaVers中,他们之间没有父母/子女关系(实际上是任何种类的关系)。 这就是为什么withChildValueObjects()过滤器不适用于此。 它只适用于ValueObjects拥有Entities,请参阅http://javers.org/documentation/jql-examples/#child-value-objects-filter

尽管如此,有两种方法可以改善您的查询。

  1. 直接询问您要跟踪的实体实例。

  2. 在查询范围中使用新的Shadow API,请参见http://javers.org/documentation/jql-examples/#query-for-shadows 这是一项新功能,将在功能上得到改进。 如果查询选择了两个实体的快照,则可以使用它。

见下面的代码:

def "should ... "(){ 
    given: 
    Javers javers = JaversBuilder.javers().build() 
    Employee bob = new Employee("Bob", 9_000, "Scrum master") 
    javers.commit("hr.manager", bob) 

    // do some changes and commit 
    bob.setPosition("Team Lead") 
    bob.setSalary(11_000) 
    javers.commit("hr.director", bob) 

    bob.addSubordinates(new Employee("Trainee One"), new Employee("Trainee Two")) 
    javers.commit("hr.manager", bob) 

    bob.getSubordinates().get(0).setAge(42) // <<<< This is the change I want to detect 
    bob.salary++ // ! 
    javers.commit("hr.manager", bob) 

    when: "ask for the the right Entity instance" 
    List changes = javers.findChanges(
      QueryBuilder.byInstanceId("Trainee One", Employee.class).build()) 

    then: 
    println(javers.processChangeList(changes, new SimpleTextChangeLog())) 
    true 

    when: "use the new Shadows" 
    List shadows = javers.findShadows(
      QueryBuilder.byInstanceId("Bob", Employee.class) 
         .withShadowScopeDeep().build()) 

    then: 
    shadows.each { 
     assert it.get() instanceof Employee 
    } 
    Employee lastBobShadow = shadows[0].get() 
    assert shadows[0].commitMetadata.id.majorId == 4 
    assert lastBobShadow.subordinates[0].name == "Trainee One" 
    assert lastBobShadow.subordinates[0].age == 42 
} 
+0

阴影物体看起来很有希望。用@ ValueObject来注释所有的“依赖”类是否足够了?有了我自己的测试类,这似乎就足够了,尽管我确实收到了一些日志消息,比如“添加缺少的父项:2.0 - > ...” - 是一个问题吗? –

+0

是的,映射相关对象作为@ValueObject应该做的工作。此日志消息只是一个嘈杂的调试消息。别担心。它应该在下一个版本中删除。 thanx报告 –

相关问题