Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data MongoDB: How ignore unique indexed field when Document is embedded in another one?

I have a Contract class defined like this:

@Document
public class Contract {

    @Id
    private String id;

    @Indexed(unique = true)
    private String ref;

    private String status = "pending";

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

I want to save contract state over time, so I created a Version class like this:

@Document
public class Version {

    @Id
    private String id;

    private Contract contract;

    private Instant createdAt;

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

When I try to save multiple times the version object over time, I have a duplicate keys exception. I think it's the duplicate key index on contract's ref which complains here.

How can I achieve this kind of thing?

like image 415
Franck Anso Avatar asked Apr 26 '16 14:04

Franck Anso


1 Answers

Simply add @Reference like this:

@Document
public class Version {

  @Id
  private String id;

  @Reference
  private Contract contract;

  private Instant createdAt;

  // getter & setter & hashcode & equals & tostring...
}
like image 189
VK321 Avatar answered Oct 27 '22 01:10

VK321