2017-06-06 5 views
0

JaVersを使用してJavaオブジェクトの変更を追跡したいと考えています。基本的な例はうまくいきますが、コレクションに格納されているオブジェクトの変更を検出することはできません。JaVersがコレクション内のオブジェクト属性の変更を検出しない

例を変更してChangeLogExample.classの例を拡張すると、サブ縦1:

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インスタンスに変更を個別にコミットするときに訓練生の年齢に変更を加えますが、それは私が期待していたものではなく、私が望むものでもありません。

私の質問は、どのように変更履歴に表示される年齢の変更を取得するのですか? 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

、[OK]を、いくつかのテストです:


私はJaVers 3.2.0

EmployeeクラスはJaVers例から変更されていませんを使用していますここに問題があります。 まず、EmpolyeeオブジェクトはEntitiesとしてマップされます。したがって、JaVersでは、それらの間に親/子関係(実際にはあらゆる種類の関係)がありません。 そのため、withChildValueObjects()フィルタはここでは適用されません。 Entitiesが所有するValueObjectsの場合にのみ有効です。http://javers.org/documentation/jql-examples/#child-value-objects-filter

なお、クエリを改善する2つの方法があります。

  1. トレースするEntityインスタンスを直接問い合わせてください。

  2. クエリスコープで新しいシャドウ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としてマッピングするとジョブが実行されます。このログメッセージは騒々しいデバッグメッセージです。それについて心配しないでください。次のリリースで削除する必要があります。そのことを報告してくれてありがとう –

関連する問題