gpt4 book ai didi

java - JaVers 未检测到集合内对象属性的更改

转载 作者:行者123 更新时间:2023-11-30 06:40:58 25 4
gpt4 key购买 nike

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

如果我延长 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 中的测试

最佳答案

好的,这里有一些问题。首先,Empolyee 对象被映射为Entities。所以在 JaVers 中,没有他们之间的父/子关系(实际上任何类型的关系)。这就是为什么 withChildValueObjects() 过滤器在这里不适用。它仅适用于 Entities 拥有的 ValueObjects,请参阅 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
}

关于java - JaVers 未检测到集合内对象属性的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44386270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com