Java Javer未检测到集合内对象属性的更改
我想使用JaVers来跟踪Java对象的变化。基本示例运行良好,但我无法让它检测存储在集合中的对象的更改 如果我扩展示例以更改,例如其中一个子坐标:Java Javer未检测到集合内对象属性的更改,java,javers,Java,Javers,我想使用JaVers来跟踪Java对象的变化。基本示例运行良好,但我无法让它检测存储在集合中的对象的更改 如果我扩展示例以更改,例如其中一个子坐标: public static void main(String[] args) { Javers javers = JaversBuilder.javers().build(); Employee bob = new Employee("Bob", 9_000, "Scrum master" ); javers.commit
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);
}
因此,对第一个下属年龄的更改不会显示在更改日志中
在ChildValueObjects()中使用没有任何区别
当我将更改单独提交给Employee实例时,我得到了对受训人员年龄的更改,但这不是我期望的(也不是我想要的)
所以我的问题是:如何让年龄的变化显示在变更日志中
我使用的是JaVers 3.2.0
Employee
类与JaVers示例中的相同:
main()
方法只是来自的测试。好,这里有几个问题。
首先,Empolyee
对象映射为实体
。因此,在贾维斯,没有
它们之间的父/子关系(实际上是任何类型的关系)。
这就是为什么withChildValueObjects()
过滤器在这里不适用的原因。
它仅适用于实体所拥有的ValueObjects
,请参阅
不过,有两种方法可以改进您的查询
直接询问要跟踪的实体实例
将新的ShadowAPI用于查询范围,请参见
这是一项新功能,将在该功能中进行改进。
如果查询将选择两个实体的快照,则可以使用它
见下面的代码:
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
}
def“应该…”(){
鉴于:
Javers Javers=JaversBuilder.Javers().build()
员工bob=新员工(“bob”,9_000,“Scrum大师”)
提交(“人力资源经理”,鲍勃)
//做一些更改并提交
bob.setPosition(“团队领导”)
bob.setSalary(11_000)
javers.commit(“人力资源总监”,bob)
bob.添加下属(新员工(“实习生一”)、新员工(“实习生二”))
提交(“人力资源经理”,鲍勃)
bob.getsubstances().get(0).setAge(42)//阴影对象看起来很有希望。用@ValueObject
注释所有“依赖”类就足够了吗?用我自己的测试类,这似乎足够了,尽管我确实收到一些日志消息,如“添加缺少的父对象:2.0->…”-这是一个问题吗?是的,将依赖对象映射为@ValueObject应该可以完成这项工作。此日志消息只是一条嘈杂的调试消息。别担心。它应该在下一版本中删除。谢谢您的报告
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
}