Spring 数据 Joda 持续时间类型存储在 mongoDB
Spring Data Joda Time Duration Type store in mongoDB
我有一个持续时间容器,看起来像这样,我也可以毫无问题地将这个容器存储在我的 MongoDB 中。但是,如果我从数据库中读取对象,则会出现以下异常:
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate org.joda.time.Duration using constructor NO_CONSTRUCTOR with arguments
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:249)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230)
有谁知道我做错了什么或者我该如何解决这个问题?
public class ScheduleDurationContainer {
@JsonView(RestServiceResponseView.SchedulerPublic.class)
private Duration duration;
...
public ScheduleDurationContainer() {
}
在我看来,您的错误是由于 Joda Time 的 Duration 没有默认的 no-arg 构造函数。您需要为 Duration 编写一个 Mongo 读取转换器。 Spring 有几个例子 here
我有一个持续时间容器,看起来像这样,我也可以毫无问题地将这个容器存储在我的 MongoDB 中。但是,如果我从数据库中读取对象,则会出现以下异常:
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate org.joda.time.Duration using constructor NO_CONSTRUCTOR with arguments at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:249) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230)
有谁知道我做错了什么或者我该如何解决这个问题?
public class ScheduleDurationContainer {
@JsonView(RestServiceResponseView.SchedulerPublic.class)
private Duration duration;
...
public ScheduleDurationContainer() {
}
在我看来,您的错误是由于 Joda Time 的 Duration 没有默认的 no-arg 构造函数。您需要为 Duration 编写一个 Mongo 读取转换器。 Spring 有几个例子 here