【问题标题】:Spring Data MongoDB - Add to db if not exist for DbRef fieldSpring Data MongoDB - 如果 DbRef 字段不存在,则添加到 db
【发布时间】:2016-05-23 14:00:32
【问题描述】:

我有一个关于spring-data-mongodb的小问题:

我有 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;
}

并假设我通过设置一个不在 db 上持久化的新 ProducerDocument 来新建类。

当我从 db 检查产品文档时,它被完美地持久化了:

但是,新的生产者对象不会被持久化到生产者集合中。我的意思是,如果您从生产者集合中检查生产者 ID,它会返回 null。

我如何设置spring mongodb,如果值是新的,也添加到引用的集合中。

谢谢。

【问题讨论】:

    标签: java mongodb spring-boot document spring-data-mongodb


    【解决方案1】:

    Spring Data MongoDB 不会将操作级联到引用的对象。因此,没有配置选项可以实现所需的行为。请参阅reference documentation 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2015-10-31
      • 1970-01-01
      • 2015-02-19
      相关资源
      最近更新 更多