question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring Data Mongo 2.7.4 - repository insert method ignores @MongoId

See original GitHub issue

WHAT

Spring Data Mongo repositories API -> insert(...) ignores @MongoId annotation with variable type and always auto-generates new ObjectId for the provided entity.

VERSION

org.springframework.boot: 2.7.4 io.spring.dependency-management: 1.0.14.RELEASE

EXPECTED BEHAVIOUR

repository.insert(...) - should create an entity in Mongo DB according to the configuration & variable type of provided @MongoId annotation inputs. In case of the existance of the entity - throw org.springframework.dao.DuplicateKeyException exception.

Issue not reproduced for: io.spring.dependency-management: 1.0.13.RELEASE

CODE SAMPLE

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) { runApplication<DemoApplication>(*args) }

@Service
class UserService(
    private val userRepository: UserRepository
) {
    @PostConstruct
    fun init() {
        val user = User(1, "Test")

        // success: Response -> id == 1, DB -> id == 1
        userRepository.save(user) 

        // success (no error): Response -> id == 1, DB -> id == [auto-gen] new ObjectId()
        userRepository.insert(user) 
    }
}

@Repository
interface UserRepository: MongoRepository<User, Long>

@Document
class User(
    @MongoId val id: Long,
    @Field val name: String
)

RESULTED MONGO DATA

Mongo version: 4.4.13

image

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Oct 7, 2022

Investigating further, as far as I can tell the version of the dependency management plugin isn’t relevant.

With Spring Boot 2.7.4 the insert succeeds when it should not. With 2.7.3 it fails with the following:

Caused by: com.mongodb.MongoWriteException: Write operation error on server localhost:27017. Write error: WriteError{code=11000, message='E11000 duplicate key error collection: test.user index: _id_ dup key: { _id: 1 }', details={}}.
        at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1018) ~[mongodb-driver-sync-4.6.1.jar:na]
        at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:471) ~[mongodb-driver-sync-4.6.1.jar:na]
        at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:454) ~[mongodb-driver-sync-4.6.1.jar:na]
        at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:448) ~[mongodb-driver-sync-4.6.1.jar:na]
        at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocument$15(MongoTemplate.java:1552) ~[spring-data-mongodb-3.4.2.jar:3.4.2]
        at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:598) ~[spring-data-mongodb-3.4.2.jar:3.4.2]
        ... 60 common frames omitted

The failure also occurs with Spring Boot 2.7.4 when Spring Data is downgraded to the version used by Spring Boot 2.7.3:

ext['spring-data-bom.version'] = "2021.2.2"

In short, this appears to be a regression in Spring Data MongoDB which https://github.com/spring-projects/spring-data-mongodb/issues/4184 is already tracking. I’m going to close this issue in favor of it.

0reactions
ishugaliycommented, Oct 7, 2022

@wilkinsona great, thanks for the quick response to the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Data MongoDB - Reference Documentation
Aggregation framework support via repository query methods. Declarative reactive transactions using @Transactional. Template API delete by ...
Read more >
How to disable spring-data-mongodb autoconfiguration in ...
I need to connect to two different MongoDB servers. So I need to configure two sets of instances for mongo connection, MongoTemplate etc....
Read more >
Spring Boot Integration With MongoDB Tutorial
In this tutorial, we demonstrate Spring Boot integration with MongoDB, connect to Atlas cluster, and perform simple CRUD examples.
Read more >
Spring Data MongoDB Transactions - Baeldung
After we finished the configuration, all we need to do to use native MongoDB transactions – is to annotate our method with @Transactional....
Read more >
Spring Data MongoDB: Custom repository implementation
In this post we'll see how to define custom spring-data repository methods and their implementation for a book repository in MongoDB and how ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found