首页 > 解决方案 > Javers - java.lang.ClassCastException: org.javers.core.diff.changetype.ValueChange 不能转换为 org.javers.core.diff.changetype.map.MapChange

问题描述

我在使用 Spring Boot Javers 示例时遇到错误。

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at com.example.SpringbootJaVersApplication.main(SpringbootJaVersApplication.java:37) [classes/:na]
Caused by: java.lang.ClassCastException: org.javers.core.diff.changetype.ValueChange cannot be cast to org.javers.core.diff.changetype.map.MapChange
    at com.example.SpringbootJaVersApplication.withJavers(SpringbootJaVersApplication.java:118) [classes/:na]
    at com.example.SpringbootJaVersApplication.run(SpringbootJaVersApplication.java:49) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    ... 5 common frames omitted

代码:

List<Change> snapshots = javers.findChanges(QueryBuilder.byInstanceId(c.getId(), Car.class).build());
for (Change ch : snapshots) {
    if(ch instanceof ValueChange) {
        System.out.println("Change is an instance of value change...");
        ValueChange vc = (ValueChange) ch;
        System.out.println(vc.getPropertyName());
        System.out.println(vc.getAffectedGlobalId());
        System.out.println(vc.getLeft()+"  "+vc.getRight());
    }

    if(ch instanceof PropertyChange) {
        PropertyChange pc = (PropertyChange)ch;
        System.out.println(pc.getPropertyName());
        System.out.println(pc.getAffectedGlobalId());

        MapChange mc = (MapChange)pc;
        System.out.println(mc.getPropertyName());
    }
}

标签: javaspring-bootjavers

解决方案


错误消息说就是,你不能从这个PropertyChange对象转换为MapChange,因为MapChange它是 的子类PropertyChange,而这个对象不是。还有许多其他子类,例如ValueChange

在此处输入图像描述


推荐阅读