spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

Update fails on DocumentReference annotated list

Open kusamau opened this issue 3 years ago • 1 comments

Assume the following class

@Document(collection = "testDocuments")
public class TestDocument {
    @Id
    private UUID id;

    @DocumentReference
    private List<TestDocument> children;
}

then the following test fails

    @Test
    void testDocumentChildrenTest(@Autowired MongoTemplate mongoTemplate) {
        TestDocument parent = new TestDocument();
        parent.setChildren(new ArrayList<>());
        mongoTemplate.save(parent);

        TestDocument child = new TestDocument();
        mongoTemplate.save(child);

        Query query = new Query(Criteria.where("id").is(parent.getId()));

        Update update = new Update();
        update.push("children").each(new Object[]{child});
        var result = mongoTemplate.update(TestDocument.class)
                .matching(query)
                .apply(update)
                .first();

        TestDocument updated = mongoTemplate.findOne(query, TestDocument.class);
        Assertions.assertEquals(1, updated.getChildren().size());
    }

An identical test, replacing @DocumentReference with @DBRef succeeds.

kusamau avatar May 04 '22 12:05 kusamau

Thanks for bringing this up! We'll have a look.

christophstrobl avatar May 06 '22 09:05 christophstrobl