Javers - 如何使用 Spring Boot 获取单个记录的所有更改值?

Javers - How to get all changed values of a single record using Spring Boot?

我正在研究 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 不起作用。

我的汽车模型 class 应该如下所示

@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;
}