有對象的經典POJO如下:春MongoDB的數據不能用「查找」查詢獲取@DBRef對象
@Document
public class MyPojo {
@DBRef
@Field("otherPojo")
private List<OtherPojo> otherPojos;
}
而且OtherPojo.java
:
public class OtherPojo{
@Id
private ObjectId _id;
private String someOtherFields;
}
我無法級聯保存這些,但我通過首先保存DBRefs然後保存我的POJO列表來解決它,但仍然當我嘗試獲取所有列表或使用下面的代碼查詢它們中的一些時:
Query query = new Query(Criteria.where("myPojo.blabla").is("blabla"));
List<MyPojo> resultList = mongoTemplate.find(query, MyPojo.class, "myCollection");
返回我空的DBREFS列表,它計算真實。例如:保存了10個DBRef,它返回10個空對象,但其基元類型和其他類型不是DBRref都是非空的。 我該如何處理?
我我的對象保存如下:
for (MyPojo pojo : somePojoList) {
for (OtherPojo otherPojo : pojo.getOtherPojos()) {
mongoTemplate.save(otherPojo, "myCollection");
}
}
// ...
mongoTemplate.insert(myPojoList, "myCollection");
編輯: OK,現在我知道,當我節省otherPojos我不指定集合名稱,我可以接他們(感謝@ jmen7070)。但是我必須在那裏寫myCollection,因爲我總是放棄並重新創建它們。這是一個用例。那麼我怎麼能說「找到方法來使用相同的集合來獲取DBRefs」?
你能提供有關你如何保存在數據庫中的對象更詳細? – jmen7070
更新了我的問題.. –