Java Spring MongoDB 数据无法通过“查找”查询获取@DBRef 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23359797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MongoDB data can not fetch @DBRef objects with "find" query
提问by Sercan Ozdemir
There is an object a classic POJO as below:
有一个经典的 POJO 对象,如下所示:
@Document
public class MyPojo {
@DBRef
@Field("otherPojo")
private List<OtherPojo> otherPojos;
}
And OtherPojo.java
:
并且OtherPojo.java
:
public class OtherPojo{
@Id
private ObjectId _id;
private String someOtherFields;
}
I can not cascade-save these, but I'm getting over it by saving DBRefs first and then saving my POJO list, but still when I try to fetch all list or querying some of them with below code:
我无法级联保存这些,但我通过先保存 DBRefs 然后保存我的 POJO 列表来克服它,但是当我尝试获取所有列表或使用以下代码查询其中一些列表时:
Query query = new Query( Criteria.where( "myPojo.blabla" ).is( "blabla" ) );
List<MyPojo> resultList = mongoTemplate.find( query, MyPojo.class, "myCollection" );
it returns me with a list of null DBrefs, it counts true. For example: there are saved 10 DBRefs it returns 10 null object in it, but its primitive types and other types that aren't a DBRref are all non-null. How can i handle this ?
它返回给我一个空 DBrefs 列表,它是真的。例如:保存了10个DBRefs,其中返回10个空对象,但是它的原始类型和其他不是DBRref的类型都是非空的。我该如何处理?
I save my objects as below:
我保存我的对象如下:
for (MyPojo pojo : somePojoList) {
for (OtherPojo otherPojo : pojo.getOtherPojos()) {
mongoTemplate.save(otherPojo, "myCollection");
}
}
// ...
mongoTemplate.insert( myPojoList, "myCollection" );
EDIT:OK, now I know if I do not specify a collection name when I'm saving otherPojos, I can fetch them (thanks to @jmen7070). But I have to write myCollection there because I'm dropping and re-creating them always. That's a use-case. So how can I say "find method to use same collection to fetch DBRefs"?
编辑:好的,现在我知道如果我在保存 otherPojos 时没有指定集合名称,我可以获取它们(感谢 @jmen7070)。但是我必须在那里写 myCollection 因为我总是丢弃和重新创建它们。这是一个用例。那么我怎么能说“使用相同集合来获取 DBRef 的查找方法”呢?
采纳答案by jmen7070
As you can see from docs:
正如您从文档中看到的:
The mapping framework does not handle cascading saves. If you change an Account object that is referenced by a Person object, you must save the Account object separately. Calling save on the Person object will not automatically save the Account objects in the property accounts.
映射框架不处理级联保存。如果更改了 Person 对象引用的 Account 对象,则必须单独保存 Account 对象。对 Person 对象调用 save 不会自动将 Account 对象保存在属性帐户中。
So, first, you have to save each object of the otherPojos list. After that you can save MyPojo instance:
因此,首先,您必须保存 otherPojos 列表的每个对象。之后,您可以保存 MyPojo 实例:
MyPojo pojo = new MyPojo();
OtherPojo otherPojo = new OtherPojo();
OtherPojo otherPojo1 = new OtherPojo();
pojo.setOtherPojos(Arrays.asList(otherPojo, otherPojo1));
mongoTemplate.save(otherPojo);
mongoTemplate.save(otherPojo1);
mongoTemplate.save(pojo);
UPDATED:You saved a objects:
更新:您保存了一个对象:
for( MyPojo pojo : somePojoList ){
for( OtherPojo otherPojo : pojo.getOtherPojos() ){
mongoTemplate.save( otherPojo,collectionname );
}
}
All otherPojo objects will be saved in collection with name "collectionName"..
所有其他Pojo对象将保存在名为“collectionName”的集合中。
but your myPojo objects have a $ref to an otherPojo collection..
但是你的 myPojo 对象有一个指向 otherPojo 集合的 $ref ..
"otherPojo" : [
{
"$ref" : "otherPojo",
"$id" : ObjectId("535f9100ad52e59815755cef")
},
{
"$ref" : "otherPojo",
"$id" : ObjectId("535f9101ad52e59815755cf0")
}
]
So, "collectionname" variable
所以,“集合名称”变量
mongoTemplate.save( otherPojo,collectionname );
must be "otherPojo".
必须是“otherPojo”。
To avoid confusion, I propose to specify a collection for saving OtherPojo objects with @Doucument annotation:
为了避免混淆,我建议使用@Doucument 注释指定一个用于保存 OtherPojo 对象的集合:
@Document(collection="otherPojos")
public class OtherPojo{
@Id
private ObjectId _id;
private String someOtherFields;
}
And to save otherPojo objects by using a overloaded save() method of mongoTemplate
并通过使用 mongoTemplate 的重载 save() 方法保存其他 Pojo 对象
mongoTemplate.save( otherPojo );
In this way , you will have a valid $ref for myPojo documents
这样,您将拥有 myPojo 文档的有效 $ref
UPDATE 2:
更新 2:
In this case, you want to store parents and childs object in same collection.
在这种情况下,您希望将父对象和子对象存储在同一个集合中。
To achieve this you can use this approach
要实现这一点,您可以使用这种方法