简体   繁体   English

Spring Data MongoDB-如果DbRef字段不存在,则添加到db

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

I have a little question about spring-data-mongodb: 我对spring-data-mongodb有一个小疑问:

I have ProductDocument class: 我有ProductDocument类:

@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. 并假设我通过设置一个新的ProducerDocument而不是在db上持久化来创建类。

When I check the product document from db, it is persisted perfectly: 当我从db检查产品文档时,它会完美保存:

在此处输入图片说明

However, new producer object is not persisted to producer collection. 但是,新的生产者对象不会持久化到生产者集合中。 I mean if you check the producer id from producer collection, it returns null. 我的意思是,如果您从生产者集合中检查生产者ID,则它将返回null。

How can I set spring mongodb that if the value is new, add to the referenced collection too. 我如何设置spring mongodb,如果值是新的,也要添加到引用的集合中。

Thanks. 谢谢。

Spring Data MongoDB does not cascade operations to referenced objects. Spring Data MongoDB不会将操作级联到引用的对象。 Thus there is no configuration option do achieve the desired behaviour. 因此,没有配置选项可以实现所需的性能。 Please see the reference documentation for more information. 请参阅参考文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM