You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I think it's a good idea to use Kotlin's inline class to enforce a restriction at the compile time and prevent yourself from mixing up an id of one entity with id on another entity.
Usually the document class looks like this:
@Document
data classPerson(@Id varid:String? = null, valname:String)
And you can pass any String to repo's findById method.
I would like to use an inline class for @Id as follows:
@JvmInline
value classPersonId(valvalue:String)
@Document
data classPerson(@Id varid:PersonId? = null, valname:String)
The problem is that it's not fully supported. The repository's save operation works fine (as far as I can see by inspecting my local Mongo instance), but, for example, findAll and findByIdOrNull fail to act as expected in different manners.
Below is an example source code, which can be put into a single test file of a project scaffolded with start.spring.io:
importorg.assertj.core.api.Assertions.assertThatimportorg.junit.jupiter.api.BeforeEachimportorg.junit.jupiter.api.Nestedimportorg.junit.jupiter.api.Testimportorg.springframework.beans.factory.annotation.Autowiredimportorg.springframework.boot.test.autoconfigure.data.mongo.DataMongoTestimportorg.springframework.data.annotation.Idimportorg.springframework.data.mongodb.core.mapping.Documentimportorg.springframework.data.mongodb.repository.MongoRepositoryimportorg.springframework.data.repository.findByIdOrNullimportorg.springframework.stereotype.Repository
@JvmInline
value classPersonId(valvalue:String)
@Document
data classPerson(@Id varid:PersonId? = null, valname:String)
@Repository
interfacePersonRepository : MongoRepository<Person, PersonId>
@DataMongoTest
classPersonRepositoryTest(@Autowired privatevalrepo:PersonRepository) {
@Test
fun`successfully saves person`() {
val newPerson =Person(name ="Bob")
val savedPerson = repo.insert(newPerson)
assertThat(savedPerson.id).isNotNull
}
@Nested
innerclassWhenPersonSavedToRepository {
privatevar id:PersonId?=null
@BeforeEach
funpersistNewPerson() {
val newPerson =Person(name ="Bob")
val savedPerson = repo.save(newPerson)
id = savedPerson.id
}
@Test
fun`finds all persons in the repository (unfortunately throws MappingException)`() {
val all = repo.findAll()
assertThat(all).isNotEmpty
}
@Test
fun`finds person by id (unfortunately returns nothing)`() {
val found = repo.findByIdOrNull(id!!)
assertThat(found).isNotNull
}
}
}
The text was updated successfully, but these errors were encountered:
Hi. I think it's a good idea to use Kotlin's inline class to enforce a restriction at the compile time and prevent yourself from mixing up an id of one entity with id on another entity.
Usually the document class looks like this:
And you can pass any
String
to repo'sfindById
method.I would like to use an inline class for
@Id
as follows:The problem is that it's not fully supported. The repository's
save
operation works fine (as far as I can see by inspecting my local Mongo instance), but, for example,findAll
andfindByIdOrNull
fail to act as expected in different manners.Below is an example source code, which can be put into a single test file of a project scaffolded with start.spring.io:
The text was updated successfully, but these errors were encountered: