Spring mongodb template saves in the same object

Viewed 1155

I have the model like following

@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;
}

When i do save with mongoTemplate.save(object); i see only 2 objects created in DB instead of 6. Just before save my debug lines for objects to be saved.

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]]

Any ideas why ? I feel the Index unique thing is not working somehow. I want code and brand to be unique combination.

public abstract class AbstractModel<ID extends Serializable> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private ID id;
}
2 Answers
Related