Javers - org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR: given javaClass is mapped to ValueObjectType, expected EntityType
Javers - org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR: given javaClass is mapped to ValueObjectType, expected EntityType
我遇到以下错误:我正在使用 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/"
}
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);
}
我自己解决了这个问题。我应该在 class 级别用 @Document
注释 Car class 并在归档级别注释 @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);
}
您可以在 Javers 中手动注册您的实体。您的实体必须有一个带有 org.javers.core.metamodel.annotation.Id
注释的 ID 属性
@Bean
public Javers javers(MongoTemplate mongoTemplate, JaversProperties javersProperties) {
return JaversBuilder.javers()
.(...) // your config here
.registerEntities(YourEntityClass.class, AnotherEntity.class) // here you register entities
.build();
}
我遇到以下错误:我正在使用 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/"
}
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);
}
我自己解决了这个问题。我应该在 class 级别用 @Document
注释 Car class 并在归档级别注释 @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);
}
您可以在 Javers 中手动注册您的实体。您的实体必须有一个带有 org.javers.core.metamodel.annotation.Id
@Bean
public Javers javers(MongoTemplate mongoTemplate, JaversProperties javersProperties) {
return JaversBuilder.javers()
.(...) // your config here
.registerEntities(YourEntityClass.class, AnotherEntity.class) // here you register entities
.build();
}