我无法检索Spring Data MongoDB中的第二级嵌套对象
我在MongoDB中嵌套的集合与Spring检索。想象一下这个模式
@Data
@Builder
@Document(collection = "emitted")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Emitter{
@Id
private String id;
@Field("installation")
@DocumentReference(lazy = true)
private Installaton installation;
// other fields
@Data
@Builder
@Document(collection = "installation")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Installation {
@Id
private String id;
@Field("subject")
@DocumentReference(lazy = true)
private Subject subject;
// other fields
@Data
@Builder
@Document(collection = "subject")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Subject {
@Id
private String id;
// other fields
另外,我使用MapStruct将嵌套对象字段映射到字符串,以避免循环引用引入按集合id搜索:
@ObjectFactory
public <T> T map(@NonNull final String id, @TargetType Class<T> type) {
return mongoTemplate.findById(id, type);
}
一切工作在第一层,但在嵌套层,我有这个错误:
由:org.springframework.core.convert.ConverterNotFoundException:没有找到能够从类型[org.bson.types]转换的转换器。键入[com.package.collections.Subject]org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound (GenericConversionService.java: 322)org.springframework.core.convert.support.GenericConversionService.convert (GenericConversionService.java: 195)org.springframework.core.convert.support.GenericConversionService.convert (GenericConversionService.java: 175)org.springframework.data.mongodb.core.convert.MappingMongoConverter.doConvert (MappingMongoConverter.java: 1826)org.springframework.data.mongodb.core.convert.MappingMongoConverter.doConvert (MappingMongoConverter.java: 1818)org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead (MappingMongoConverter.java: 1337)org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead (MappingMongoConverter.java: 1311)org.springframework.data.mongodb.core.convert.MappingMongoConverter DefaultConversionContext.convert美元(MappingMongoConverter.java: 2371)org.springframework.data.mongodb.core.convert.MappingMongoConverter ConversionContext.convert美元(MappingMongoConverter.java: 2174)org.springframework.data.mongodb.core.convert.MappingMongoConverter MongoDbPropertyValueProvider.getPropertyValue美元(MappingMongoConverter.java: 1936)org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties (MappingMongoConverter.java: 638)org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties (MappingMongoConverter.java: 549)org.springframework.data.mongodb.core.convert.MappingMongoConverter.read (MappingMongoConverter.java: 527)org.springframework.data.mongodb.core.convert.MappingMongoConverter.readDocument (MappingMongoConverter.java: 491)org.springframework.data.mongodb.core.convert.MappingMongoConverter.read (MappingMongoConverter.java: 427)org.springframework.data.mongodb.core.convert.MappingMongoConverter.read (MappingMongoConverter.java: 423)org.springframework.data.mongodb.core.convert.MappingMongoConverter.read (MappingMongoConverter.java: 120)org.springframework.data.mongodb.core.MongoTemplate ReadDocumentCallback.doWith美元(MongoTemplate.java: 3326)org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal (MongoTemplate.java: 2940)org.springframework.data.mongodb.core.MongoTemplate.doFindOne (MongoTemplate.java: 2618)org.springframework.data.mongodb.core.MongoTemplate.doFindOne (MongoTemplate.java: 2588)org.springframework.data.mongodb.core.MongoTemplate.findById (MongoTemplate.java: 922)在com.package.myapp.services.mapper.ReferenceMapper.map (ReferenceMapper.java: 26)com.package.myapp.services.mapper.InstallationMapperImpl.toEntity (InstallationMapperImpl.java: 102)
当请求转换时,findById工作正确并检索对象和嵌套对象。当请求是第二层嵌套对象时,它失败,其中ObjectId被检索但不能转换并且失败。
我在回答自己,因为我找到了适合我问题的解决方案。
我只需要带有id的实体对象,所以我编写了一个转换器:public class ObjectIdToSubjectConverter implements Converter<ObjectId, Subject> {
@Override
public Subject convert(ObjectId source) {
return Subject.builder().id(source.toString()).build();
}
}
并将其注册为bean:
@Configuration
public class MongoConfig {
@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(Collections.singletonList(new ObjectIdToSubjectConverter());
}
}