我正在研究Spring Boot Javers示例。在此的示例horsePower属性的值已更改。现在我想知道什么是旧值,什么是当前值。

db.getCollection('jv_snapshots').find({})
/* 1 */
{
    "_id" : ObjectId("5d0602e476e79c53f06f1d6b"),
    "commitMetadata" : {
        "author" : "unauthenticated",
        "properties" : [],
        "commitDate" : "2019-06-16T14:20:44.465",
        "commitDateInstant" : "2019-06-16T08:50:44.465Z",
        "id" : NumberLong(1)
    },
    "globalId" : {
        "valueObject" : "com.example.model.Car"
    },
    "state" : {
        "horsePower" : NumberLong(670),
        "year" : "2015",
        "model" : "488",
        "id" : "5d0602e476e79c53f06f1d6a",
        "brand" : "Ferrari"
    },
    "changedProperties" : [
        "horsePower",
        "year",
        "model",
        "id",
        "brand"
    ],
    "type" : "INITIAL",
    "version" : NumberLong(1),
    "globalId_key" : "com.example.model.Car/"
}

/* 2 */
{
    "_id" : ObjectId("5d0602e476e79c53f06f1d6d"),
    "commitMetadata" : {
        "author" : "unauthenticated",
        "properties" : [],
        "commitDate" : "2019-06-16T14:20:44.574",
        "commitDateInstant" : "2019-06-16T08:50:44.574Z",
        "id" : NumberLong(2)
    },
    "globalId" : {
        "valueObject" : "com.example.model.Car"
    },
    "state" : {
        "horsePower" : NumberLong(800),
        "year" : "2015",
        "model" : "488",
        "id" : "5d0602e476e79c53f06f1d6a",
        "brand" : "Ferrari"
    },
    "changedProperties" : [
        "horsePower"
    ],
    "type" : "UPDATE",
    "version" : NumberLong(2),
    "globalId_key" : "com.example.model.Car/"
}


我已经开发了这样的代码,但是效果不佳。

@Autowired
    private Javers javers;

    private void withJavers() {
        List<Change> changes = javers.findChanges(QueryBuilder.byClass(Car.class)
                .withNewObjectChanges().build());

        System.out.println("Printing the flat list of Changes :");
        changes.forEach(change -> System.out.println("- " + change));
    }

最佳答案

我发现了问题,并能够解决此问题。 globalId_key" : "com.example.model.Car/未附加PK或Id值(例如com.example.model.Car/5d124ec44f9e8e52d444aa4b),因此javers.findChanges不起作用。

我的汽车模型课应该像下面这样

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@TypeAlias("Car")
@Document
public class Car {
    @Id
    private String id;
    private String brand;
    private String year;
    private String model;
    private Long horsePower;
    @Version
    private String version;
}

10-08 13:09