Does org.springframework.data.repository.CrudRepository (with MongoDB) supports updating object that is already persisted via repository "save" method?
First of all, if object had been already persisted, call to "save" method with equal object should do nothing (there should not be two equal objects stored in DB)
Something like in the snippet below:
Code:CrudRepository<MyEntity> repo = (...) MyEntity entity = new MyEntity("key-123", "xxx"); MyEntity entity2 = new MyEntity("key-123", "xxx"); // now we have two equal objects repo.save(entity); repo.save(entity2); // nothing is done because this entity has been already persisted in DB
Also, if object has changed, for example:
Repository should find entity with key "key-123" and update it with value "xxx"->"yyy" instead of creating second document MyEntity("key-123", "yyy").Code:repo.save(new MyEntity("key-123", "yyy")
From the tests, I've done it seems that CrudRepository #save method always persist object to db, even if equal object has been already persisted. So for example:
would create 10 documents in Mongo database.Code:for (int i=0; i<10; i++) { repository.save(new MyEntity("key-123", "xxx"); }
But maybe I'm doing something wrong, or it is some way to prevent such behavior.
(of course MyEntity has hasCode and equals methods overridden)
Thanks in advance,
Piotr


Reply With Quote
