Spring MongoDB data cannot retrieve @DBRef objects with a find query

There is a classic POJO object, as shown 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 cannot cascade - save them, but I go to it, first saving DBRefs and then saving the POJO list, but still when I try to get the whole list or request some of them using the code below:

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 considers true. For example: 10 DBRefs are stored, it returns 10 null objects in it, but its primitive types and other types that are not DBRref are not null. How can I handle this?

I save my objects as shown below:

for (MyPojo pojo : somePojoList) {
    for (OtherPojo otherPojo : pojo.getOtherPojos()) {
        mongoTemplate.save(otherPojo, "myCollection");
    }
}

// ...

mongoTemplate.insert( myPojoList, "myCollection" );

: , , , otherPojos, ( @jmen7070). myCollection , . . , " DBRefs"?

+4
1

docs:

. Account, Person, Account . Person .

, -, 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);

: :

for( MyPojo pojo : somePojoList ){
            for( OtherPojo otherPojo : pojo.getOtherPojos() ){
                mongoTemplate.save( otherPojo,collectionname );
            }
        }

otherPojo "collectionName"..

myPojo $ref Pojo..

"otherPojo" : [ 
        {
            "$ref" : "otherPojo",
            "$id" : ObjectId("535f9100ad52e59815755cef")
        }, 
        {
            "$ref" : "otherPojo",
            "$id" : ObjectId("535f9101ad52e59815755cf0")
        }
    ]

, "collectionname"

 mongoTemplate.save( otherPojo,collectionname );

"otherPojo".

, OtherPojo @Doucument:

@Document(collection="otherPojos")
public class OtherPojo{

@Id
private ObjectId _id;
private String someOtherFields;

}

Pojo save() mongoTemplate

mongoTemplate.save( otherPojo );

, $ref myPojo

2:

.

+2

Source: https://habr.com/ru/post/1538543/


All Articles