我有類似的模型以下春MongoDB的模板保存在同一個對象
@CompoundIndexes(value = {
@CompoundIndex(name = "catalog_idx", def = "{'code' : 1, 'brand' : 1}", unique = true) })
@Document(collection = Catalog.ENTITY)
public class Catalog extends AbstractModel<String> {
private static final long serialVersionUID = 1L;
public static final String ENTITY = "catalog";
@NotNull(message = "Code is required")
@Field("code")
private String code;
@NotNull(message = "Brand is required")
@DBRef(lazy = true)
@Field("brand")
private Brand brand;
}
當我與mongoTemplate.save(object);
救我只看到2 DB,而不是6只創建之前保存的對象我調試行對象被保存。
Catalog [code=StagedCatalog, brand=Brand [code=Brand_3]]
Catalog [code=StagedCatalog, brand=Brand [code=Brand_2]]
Catalog [code=StagedCatalog, brand=Brand [code=Brand_1]]
Catalog [code=OnlineCatalog, brand=Brand [code=Brand_2]]
Catalog [code=OnlineCatalog, brand=Brand [code=Brand_1]]
Catalog [code=OnlineCatalog, brand=Brand [code=Brand_3]]
任何想法爲什麼?我覺得索引獨特的東西不能以某種方式工作。我想要code
和brand
爲unique combination
。
public abstract class AbstractModel<ID extends Serializable> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private ID id;
}
你有創建目錄的代碼?你有抽象模型中的「@id」列嗎?你可以記錄嗎? – wargre
@wargre did .... –