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

标签 java spring-boot javers

我正在研究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;
}

关于java - Javers - 如何使用 Spring Boot 获取单个记录的所有更改值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56618484/

相关文章:

java - 检测最后一次 foreach 循环迭代

java - JPA/获取实体列表作为引用

spring - 如何从 REST spring 应用程序的 SqlExceptionHelper 获取有关错误的信息

java - 如何使用 Javers 比较 hibernate BO 对象

java - 如何使用 spring-boot 创建 REST 服务?

java - 图像翻转不起作用

java - Java+Springboot中向远程地址发送http请求并返回响应的几种方式

java - 在 Spring Boot 插件中定义系统属性

java - 如何获取特性?

java - 使用javers库时自定义json