Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring mongodb template saves in the same object

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;
}
like image 936
Saurabh Kumar Avatar asked Aug 07 '17 08:08

Saurabh Kumar


People also ask

What is mongotemplate in spring data MongoDB?

MongoTemplate is defined by Spring Data MongoDB, used to perform DB operations without using any repository interface. If you are reading this article in context of Spring Boot, you must have heard about the term JDBCTemplate. Like JDBCTemplate, MongoTemplate is a class which provides us different handy methods to work on various DB operations.

What is @data annotation in spring data MongoDB?

@Data annotation is used from lombok to create getters and setters and toString. MongoDB stores data in collections. Spring Data MongoDB maps the Student class into a collection called student. If you want to change the name of the collection, you can use Spring Data MongoDB’s @Document annotation on the class to specify the collection name.

How to save a document to a collection in MongoDB?

Starting in MongoDB 4.2, the db.collection.save () method is deprecated. Use db.collection.insertOne () or db.collection.replaceOne () instead. A document to save to the collection.

How do I create a MongoDB database in Spring Boot?

Creating a MongoDB Database via Database seeder. With the required files and configuration for the basic application established, the database can now be created using a database seeder via spring boot. This section will cover how to populate the MongoDB database with a basic collection as a sample dataset.


Video Answer


1 Answers

You have set a unique index. It means that you will be unable to have 2 documents with the same code and brand.

Now you have set the ID column to ID object. The fact that you have 2 insert instead of 6 means that you use the same ID for 3 insert, something like :

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

To prevent that, you need to:

Catalog cat=new Catalog();
ID id=new ID(realUniqueId);  // RealuniqueId can be code+brand for instance
cat.setId(id); 
...
like image 81
wargre Avatar answered Sep 18 '22 12:09

wargre