skynyrd
skynyrd

Reputation: 982

Spring Data MongoDB - Add to db if not exist for DbRef field

I have a little question about spring-data-mongodb:

I have ProductDocument class:

@Document
public @Data class ProductDocument extends DocumentObject {
    private String name;
    private int category;
    private int unit;
    private String description;
    private double price;

    @DBRef
    private ProducerDocument producer;

    private int unitInStock;
    private int status;
    private String city;
}

And suppose that I new the class by setting a new ProducerDocument that is not persisted on db.

When I check the product document from db, it is persisted perfectly:

enter image description here

However, new producer object is not persisted to producer collection. I mean if you check the producer id from producer collection, it returns null.

How can I set spring mongodb that if the value is new, add to the referenced collection too.

Thanks.

Upvotes: 0

Views: 1825

Answers (1)

Christoph Strobl
Christoph Strobl

Reputation: 6726

Spring Data MongoDB does not cascade operations to referenced objects. Thus there is no configuration option do achieve the desired behaviour. Please see the reference documentation for more information.

Upvotes: 1

Related Questions