本文介绍了Spring Data MongoDB:当Document嵌入另一个时,如何忽略唯一索引字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Contract类定义如下:
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...
}
我想保存合同状态随着时间的推移,所以我创建了像这样的Version类:
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?
推荐答案
只需像这样添加@Reference:
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...
}
这篇关于Spring Data MongoDB:当Document嵌入另一个时,如何忽略唯一索引字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!