Javers - org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR: данный javaClass сопоставлен с ValueObjectType, ожидаемый EntityType - PullRequest
0 голосов
/ 16 июня 2019

Я получаю ошибку ниже: и я работаю с Spring Boot Mongo Javers .

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at com.example.SpringbootJaVersApplication.main(SpringbootJaVersApplication.java:28) [classes/:na]
Caused by: org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR: given javaClass 'class com.example.model.Car' is mapped to ValueObjectType, expected EntityType
    at org.javers.core.metamodel.type.TypeMapper.getJaversManagedType(TypeMapper.java:188) ~[javers-core-5.3.4.jar:na]
    at org.javers.core.metamodel.object.GlobalIdFactory.createInstanceId(GlobalIdFactory.java:115) ~[javers-core-5.3.4.jar:na]
    at org.javers.core.metamodel.object.GlobalIdFactory.createFromDto(GlobalIdFactory.java:128) ~[javers-core-5.3.4.jar:na]
    at org.javers.repository.jql.FilterDefinition$IdFilterDefinition.compile(FilterDefinition.java:27) ~[javers-core-5.3.4.jar:na]
    at org.javers.repository.jql.JqlQuery.compile(JqlQuery.java:120) ~[javers-core-5.3.4.jar:na]
    at org.javers.repository.jql.QueryCompiler.compile(QueryCompiler.java:16) ~[javers-core-5.3.4.jar:na]
    at org.javers.repository.jql.SnapshotQueryRunner.queryForSnapshots(SnapshotQueryRunner.java:30) ~[javers-core-5.3.4.jar:na]
    at org.javers.repository.jql.QueryRunner.queryForSnapshots(QueryRunner.java:44) ~[javers-core-5.3.4.jar:na]
    at org.javers.core.JaversCore.findSnapshots(JaversCore.java:191) ~[javers-core-5.3.4.jar:na]
    at com.example.SpringbootJaVersApplication.withJavers(SpringbootJaVersApplication.java:54) [classes/:na]
    at com.example.SpringbootJaVersApplication.run(SpringbootJaVersApplication.java:38) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    ... 5 common frames omitted

Данные в Монго:

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

Car.java

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

Для приведенного ниже кода ошибка получения

QueryBuilder jqlQuery = QueryBuilder.byInstanceId((Object) "5d0602e476e79c53f06f1d6d", Car.class);
        List<CdoSnapshot> cdoSnapshots = javers.findSnapshots(jqlQuery.build());
        cdoSnapshots.sort((o1, o2) -> -1 * (int) o1.getVersion() - (int) o2.getVersion());
        for (CdoSnapshot cdoSnapshot : cdoSnapshots) {
            System.out.println(cdoSnapshot);
        }

1 Ответ

0 голосов
/ 17 июня 2019

Я смог сам разобраться в проблеме. Я должен был аннотировать класс Car с @Document на уровне класса и @Id на уровне поля, чтобы создать GlobalId_key с постоянным первичным ключом документа.

Это просто решило мою проблему. В моем новом примере с приведенным ниже кодом работает!

List<Person> persons = personRepository.findAll();

        Person p = persons.get(0);
        p.setName("Ravi");
        personRepository.save(p);

        JqlQuery query = QueryBuilder.byInstanceId(persons.get(0).getId(), Person.class).build();
        List<CdoSnapshot> shadows = javers.findSnapshots(query);
        for (CdoSnapshot cdoSnapshot : shadows) {
            System.out.println(cdoSnapshot);
        }
...