首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javers org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR:给定javaClass映射到ValueObjectType,预期的EntityType

Javers org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR:给定javaClass映射到ValueObjectType,预期的EntityType
EN

Stack Overflow用户
提问于 2019-06-16 10:47:33
回答 2查看 911关注 0票数 0

我遇到了以下错误:我正在使用Spring

代码语言:javascript
复制
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

蒙古的数据:

代码语言:javascript
复制
{
    "_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

代码语言:javascript
复制
@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;
    }
}

对于下面的代码获取错误

代码语言:javascript
复制
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);
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-17 15:44:22

我自己解决了这个问题。我应该在类级别使用@Document注释Car类,在归档级别使用@Id来创建GlobalId_key,以便使用文档的持久化主键创建GlobalId_key

这只是解决了我的问题。在我的新示例中,下面的代码可以工作!

代码语言:javascript
复制
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);
        }
票数 0
EN

Stack Overflow用户

发布于 2019-11-05 09:59:01

您可以在Javer中手动注册实体。实体必须有一个带有org.javers.core.metamodel.annotation.Id注释的ID属性

代码语言:javascript
复制
@Bean
public Javers javers(MongoTemplate mongoTemplate, JaversProperties javersProperties) {
    return JaversBuilder.javers()
        .(...) // your config here
        .registerEntities(YourEntityClass.class, AnotherEntity.class) // here you register entities
        .build();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56618106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档