spring-data-mongodb 嵌入式文档上的转换器不起作用
spring-data-mongodb converters on embedded documents doesn't work
我需要阅读 文档 (ParentDocument) 包含 bean 列表 (ChildDocument ).而这些bean,i只需要转换特定的字段(content)来改变值。
它仅用于读取 mongoDb 和加载 bean,不会将修改存储在 mongodb 中。
例如:
@Document(collection = "parent")
class ParentDocument {
@Id
String id;
List<ChildDocument> children;
}
@Document
class ChildDocument {
@Id
String id;
// Modify the content reading in the database before storing the result in the java bean
String content;
}
我尝试在 ChildDocument 上使用转换器,但它不起作用,因为转换器不用于像 ChildDocument 这样的嵌入式文档(我认为?)。
有人可以帮助我并有一个干净的解决方案吗?
谢谢
我的解决方案是:
- 为父子创建文档转换器
- 为父亲注册转换器
- 在父类的Convert方法中,子类转换器必须实例化为子类必需的参数唯一来源
- 将响应分配给在转换器 father 中实例化的父文档
好吧,您只需要转换触发子转换的父文档即可。
待会见...
我需要阅读 文档 (ParentDocument) 包含 bean 列表 (ChildDocument ).而这些bean,i只需要转换特定的字段(content)来改变值。 它仅用于读取 mongoDb 和加载 bean,不会将修改存储在 mongodb 中。
例如:
@Document(collection = "parent")
class ParentDocument {
@Id
String id;
List<ChildDocument> children;
}
@Document
class ChildDocument {
@Id
String id;
// Modify the content reading in the database before storing the result in the java bean
String content;
}
我尝试在 ChildDocument 上使用转换器,但它不起作用,因为转换器不用于像 ChildDocument 这样的嵌入式文档(我认为?)。
有人可以帮助我并有一个干净的解决方案吗?
谢谢
我的解决方案是:
- 为父子创建文档转换器
- 为父亲注册转换器
- 在父类的Convert方法中,子类转换器必须实例化为子类必需的参数唯一来源
- 将响应分配给在转换器 father 中实例化的父文档
好吧,您只需要转换触发子转换的父文档即可。
待会见...