gpt4 book ai didi

java - Javers - org.javers.common.exception.JaversException : MANAGED_CLASS_MAPPING_ERROR: given javaClass is mapped to ValueObjectType, 预期 EntityType

转载 作者:行者123 更新时间:2023-12-02 04:23:52 26 4
gpt4 key购买 nike

我收到以下错误:并且我正在使用 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

Mongo 中的数据:

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

汽车.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);
}

最佳答案

我自己解决了这个问题。我应该在类级别使用 @Document 注释 Car 类,并在字段级别使用 @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);
}

关于java - Javers - org.javers.common.exception.JaversException : MANAGED_CLASS_MAPPING_ERROR: given javaClass is mapped to ValueObjectType, 预期 EntityType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56618106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com