Spring mongodb 模板保存在同一对象中
Spring mongodb template saves in the same object
我有如下模型
@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 个对象,而不是 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;
}
您设置了唯一索引。这意味着您将无法拥有 2 个具有相同代码和品牌的文件。
现在您已将 ID 列设置为 ID 对象。您有 2 个插入而不是 6 个的事实意味着您对 3 个插入使用相同的 ID,例如:
for (code: {"StagedCatalog","OnlineCatalog"} ) {
ID id=new ID(...);
for (brand: {1, 2, 3}){
Catalog cat=new Catalog();
cat.setId(id); // <<== this is wrong, you reuse the same id, you will insert first brand, then update to brand2 and brand3.
cat.setCode(code);
cat.setBrand(brand);
mongoTemplate.persist(cat);
}
}
为防止这种情况发生,您需要:
Catalog cat=new Catalog();
ID id=new ID(realUniqueId); // RealuniqueId can be code+brand for instance
cat.setId(id);
...
根据文档参数更新现有文档或插入新文档。
我有如下模型
@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 个对象,而不是 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;
}
您设置了唯一索引。这意味着您将无法拥有 2 个具有相同代码和品牌的文件。
现在您已将 ID 列设置为 ID 对象。您有 2 个插入而不是 6 个的事实意味着您对 3 个插入使用相同的 ID,例如:
for (code: {"StagedCatalog","OnlineCatalog"} ) {
ID id=new ID(...);
for (brand: {1, 2, 3}){
Catalog cat=new Catalog();
cat.setId(id); // <<== this is wrong, you reuse the same id, you will insert first brand, then update to brand2 and brand3.
cat.setCode(code);
cat.setBrand(brand);
mongoTemplate.persist(cat);
}
}
为防止这种情况发生,您需要:
Catalog cat=new Catalog();
ID id=new ID(realUniqueId); // RealuniqueId can be code+brand for instance
cat.setId(id);
...
根据文档参数更新现有文档或插入新文档。