spring-projects / spring-projects/spring-data-mongodb
Attempts to update a lazy object result in the creation of a new object rather than modifying the existing one.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
I have a user document that has a reference to subgroups as lazy. I am trying to get a subgroup from the user and then try to modify it but instead of updating it tries to insert a new one. This is only the case when I add the auditing field @Version version. If I removed the version field, everything work fine and it update the document.
// PostConstruct
public void test() {
setup(); // setting update test data
executeOperation();
}
private void executeOperation() {
final User user = userRepository.findById("1").get();
user.getSubTree().getChildren().add(subRepository.findById("p1").get());
nodeService.save(user.getSubTree());
}
private void setup() {
final MongoUser user = new MongoUser("1");
user.setName("test");
final MongoUser savedUser = userRepository.save(user);
final MongoSubGroup group = new MongoSubGroup();
group.setId("BASE");
group.setOwner(savedUser);
group.setName("BASE");
subGroupRepository.save(group);
final Dept mongoDepartment = new MongoDept("dept1");
final Dept savedDepartment = deptRepository.save(mongoDepartment);
final MongoSub profile = new MongoSub();
profile.setId("p1");
profile.setOwner(savedDepartment);
subRepository.save(profile);
}`
From executeOperation() there is a lazy load operation for subgroups and then there is a call to nodeservice
public void save(SubGroup subgrup) {
for (SubTreeNode node : subgrup.getChildren()) {
// skipping Some business logic
final Link link = new Link();
link.setId("1");
((MongoSubGroup) (subgrup)).getLinks().add(link);
}
groupRepository.save(subgrup);
}
So here as soon groupRepository.save(subgrup) tries to create a new one and as id as the index it gives duplicate error
Caused by: com.mongodb.MongoWriteException: Write operation error on server localhost:27017. Write error: WriteError{code=11000, message='E11000 duplicate key error collection: demo.subGroup index: _id_ dup key: { _id: "BASE" }', details={}}.
Attached sample project
demo.zip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the attached demo project and reproducing the failure in executeOperation(), especially the lazy subgroups access followed by nodeService.save(). Trace groupRepository.save(subgrup) through the @Version handling and lazy object mapping. Done means saving the existing subgroup with id "BASE" updates it instead of attempting a duplicate insert.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, mongodb
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100