Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. @Builder
  2. @Data
  3. @AllArgsConstructor
  4. @NoArgsConstructor
  5. public class Car {
  6. private String id;
  7. private String brand;
  8. private String year;
  9. private String model;
  10. private Long horsePower;
  11.  
  12. public Car(String brand, String year, String model, Long horsePower) {
  13. super();
  14. this.brand = brand;
  15. this.year = year;
  16. this.model = model;
  17. this.horsePower = horsePower;
  18. }
  19. }
  20.  
  21. @JaversSpringDataAuditable
  22. public interface CarRepository extends MongoRepository<Car, String>{
  23. Car findByModel(String string);
  24. List<Car> findByHorsePowerLessThan(int i);
  25. }
  26.  
  27. @SpringBootApplication
  28. @Slf4j
  29. public class SpringbootJaVersApplication implements CommandLineRunner {
  30.  
  31. public static void main(String[] args) {
  32. SpringApplication.run(SpringbootJaVersApplication.class, args);
  33. }
  34.  
  35. @Autowired
  36. private CarRepository carRepository;
  37.  
  38. @Override
  39. public void run(String... args) throws Exception {
  40. //withOutJavers();
  41.  
  42. withJavers();
  43. }
  44.  
  45. @Autowired
  46. private Javers javers;
  47.  
  48. private void withJavers() {
  49. QueryBuilder builder = QueryBuilder.byClass(Car.class).withSnapshotTypeUpdate();
  50. List<CdoSnapshot> changes = javers.findSnapshots(builder.build());
  51. log.info("Found the following updates: ");
  52. for(CdoSnapshot change: changes) {
  53. log.info("Car model snapshot: {}", change);
  54. }
  55. }
  56.  
  57. private void withOutJavers() {
  58. // Clean the collection for our test
  59. carRepository.deleteAll();
  60.  
  61. // create test data
  62. // Car car1 = new Car("Ferrari", "2015", "488", 670);
  63. // Car car2 = new Car("Fiat", "2012", "Abarth 595", 140);
  64. // Car car3 = new Car("Fiat", "2007", "Abarth 500", 135);
  65.  
  66. Car car1 = Car.builder().brand("Ferrari").year("2015").horsePower(670L).model("488").build();
  67. // Car car2 = Car.builder().brand("Fiat").year("2012").horsePower(595L).model("140").build();
  68. // Car car3 = Car.builder().brand("Ferrari").year("2013").horsePower(135L).model("500").build();
  69.  
  70. carRepository.saveAll(Arrays.asList(car1));
  71.  
  72. // find a car by model
  73. Car car488 = carRepository.findByModel("488");
  74. // modify the car
  75. car488.setHorsePower(800L);
  76. carRepository.save(car488);
  77. log.info("Car488: {}", car488);
  78.  
  79. // delete a car
  80. // Car car500 = carRepository.findByModel("Abarth 500");
  81. // carRepository.delete(car500);
  82.  
  83. // find cars with horse power less than 200
  84. List<Car> cars = carRepository.findByHorsePowerLessThan(200);
  85.  
  86. // log the results
  87. log.info("Found the following cars");
  88. for (Car foundCar : cars) {
  89. log.info(foundCar.toString());
  90. }
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement