【发布时间】:2020-03-20 03:45:14
【问题描述】:
我有两个系列:Foo 和 Bar。最后一个与 Foo 有关系 hasToMany。我想将 Foo 保存在一个集合中,将 Bar 保存在另一个集合中,但要使用嵌入的 Foo 文档,而不仅仅是使用 DBRef。
@Document
Foo {
String title;
}
@Document
Bar {
String title;
List<Foo> foos;
}
预期收藏:
Foo:
[{ObjectId("5ddad4b73679ca0ecbf3cd4b"), title: "Hello Foo 1", _class: com.play.spring.mongo.Foo},
{ObjectId("34acd4b73679ca0ecbf3cd4b"), title: "Hello Foo 2", _class: com.play.spring.mongo.Foo}]
Bar:
[{ObjectId("asdd4b73679ca0ecbf3cd4b"),
title: "Hello Bar 1"
foos: [
{ObjectId("5ddad4b73679ca0ecbf3cd4b"), title: "Hello Foo 1", _class: com.play.spring.mongo.Foo},
{ObjectId("34acd4b73679ca0ecbf3cd4b"), title: "Hello Foo 2", _class: com.play.spring.mongo.Foo}
],
_class: com.play.spring.mongo.Bar}]
但是当我保存 Bar 文档时,我得到了这个错误:
java.lang.IllegalArgumentException: Target bean of type java.util.ArrayList is not of type of the persistent entity (com.play.spring.mongo.Foo)!: java.util.ArrayList
如果我从 Foo 中删除 @Document,嵌入文档在 Bar 上工作,如果 @DBRef 添加到 Bar 中的 foos,那么它会创建从 Bar 到 Foo 的引用。但是使用@Document 这两个注释它不起作用。
【问题讨论】: