There is an object a classic POJO as below:
@Document
public class MyPojo {
@DBRef
@Field("otherPojo")
private List<OtherPojo> otherPojos;
}
And 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:
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 ?
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"?