jjzjj

java - Spring 数据 MongoDB : How ignore unique indexed field when Document is embedded in another one?

coder 2024-03-20 原文

我有一个这样定义的 Contract 类:

@Document
public class Contract {

    @Id
    private String id;

    @Indexed(unique = true)
    private String ref;

    private String status = "pending";

    // getter & setter & hashcode & equals & tostring...
}

我想随时间保存契约(Contract)状态,所以我创建了一个 Version 类,如下所示:

@Document
public class Version {

    @Id
    private String id;

    private Contract contract;

    private Instant createdAt;

    // getter & setter & hashcode & equals & tostring...
}

当我尝试随时间多次保存版本对象时,出现重复键异常。我认为这是契约(Contract) ref 上的重复键索引在这里提示。

我怎样才能实现这种事情?

最佳答案

像这样简单地添加@Reference:

@Document
public class Version {

  @Id
  private String id;

  @Reference
  private Contract contract;

  private Instant createdAt;

  // getter & setter & hashcode & equals & tostring...
}

关于java - Spring 数据 MongoDB : How ignore unique indexed field when Document is embedded in another one?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867710/

有关java - Spring 数据 MongoDB : How ignore unique indexed field when Document is embedded in another one?的更多相关文章

随机推荐