Javers-如何使用Spring Data Mongo获取mongodb中collection的更改字段的旧值?

时间:2019-06-16 09:38:38

标签: java spring-boot javers

我已经遍历了许多链接,例如https://nullbeans.com/auditing-using-spring-boot-mongodb-and-javers/Javers Comparing List In Order,但是我真的想知道文档的旧值已被更新还是旧值列表?

我已经开发了示例代码: 汽车

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
    private String id;
    private String brand;
    private String year;
    private String model;
    private Long horsePower;

    public Car(String brand, String year, String model, Long horsePower) {
        super();
        this.brand = brand;
        this.year = year;
        this.model = model;
        this.horsePower = horsePower;
    }
}

CarRepository.java

@JaversSpringDataAuditable
public interface CarRepository extends MongoRepository<Car, String>{
    Car findByModel(String string);
    List<Car> findByHorsePowerLessThan(int i);
}

SpringbootJaVersApplication.java

@SpringBootApplication
@Slf4j
public class SpringbootJaVersApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootJaVersApplication.class, args);
    }

    @Autowired
    private CarRepository carRepository;

    @Override
    public void run(String... args) throws Exception {
        //withOutJavers();

        withJavers();
    }

    @Autowired
    private Javers javers;

    private void withJavers() {
        QueryBuilder builder = QueryBuilder.byClass(Car.class).withSnapshotTypeUpdate();
        List<CdoSnapshot> changes = javers.findSnapshots(builder.build());
        log.info("Found the following updates: ");
        for(CdoSnapshot change: changes) {
            log.info("Car model snapshot: {}", change);
        }
    }

    private void withOutJavers() {
        // Clean the collection for our test
        carRepository.deleteAll();

        // create test data
        //      Car car1 = new Car("Ferrari", "2015", "488", 670);
        //      Car car2 = new Car("Fiat", "2012", "Abarth 595", 140);
        //      Car car3 = new Car("Fiat", "2007", "Abarth 500", 135);

        Car car1 = Car.builder().brand("Ferrari").year("2015").horsePower(670L).model("488").build();
        //      Car car2 = Car.builder().brand("Fiat").year("2012").horsePower(595L).model("140").build();
        //      Car car3 = Car.builder().brand("Ferrari").year("2013").horsePower(135L).model("500").build();

        carRepository.saveAll(Arrays.asList(car1));

        // find a car by model
        Car car488 = carRepository.findByModel("488");
        // modify the car
        car488.setHorsePower(800L);
        carRepository.save(car488);
        log.info("Car488: {}", car488);

        // delete a car
        //      Car car500 = carRepository.findByModel("Abarth 500");
        //      carRepository.delete(car500);

        // find cars with horse power less than 200
        List<Car> cars = carRepository.findByHorsePowerLessThan(200);

        // log the results
        log.info("Found the following cars");
        for (Car foundCar : cars) {
            log.info(foundCar.toString());
        }
    }

}

快照记录 {

    "_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/"
}
// ----------------------------------------------
{ 
    "_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/"
}

0 个答案:

没有答案