I wanted to play with Micronaut 2.5 and the latest changes in Micronaut Data allowing to work with immutable Kotlin data classes.
Can I use a non-nullable
Longas identifier when creating an entity and what happens when Micronaut Data JDBC repository saves this entity to the DB?
The answer: An entity is instantiated with some random id (for ex. Long.Max). When you call the JDBC repository
save method it will return a copy of the object, but the id will match the DB primary key.
- JDK 11
- Docker
- Micronaut 2.5.1
- Micronaut Gradle Plugin
- Kotlin
- Java 11
- Testcontainers with Postgresql
- Flyway
Most interesting things can be found in the MicronautDataIdExampleTest.kt file.
- Immutable Kotlin data class annotated with
MappedEntitywithidgenerated by a DB sequence - For testcontainer configuration see application-test.yml