Spring Data Mongo 2.7.4 - repository insert method ignores @MongoId
See original GitHub issueWHAT
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
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
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:
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.
@wilkinsona great, thanks for the quick response to the issue.