Hey guys! I am working on Spring Reactive using Ko...
# announcements
n
Hey guys! I am working on Spring Reactive using Kotlin. I am trying to find the best solution to get the
id
property value of generic
T
this is my code.
Copy code
class BaseRepository(val client: DatabaseClient, val dataAccessStrategy: ReactiveDataAccessStrategy) {

    suspend inline fun <reified T> save(entity: T): T {

        val id = // How to get the id of entity?
        try {

            return client.insert()
                         .into(T::class.java)
                         .using(entity)
                         .map(dataAccessStrategy.converter.populateIdIfNecessary(entity))
                         .awaitOne()

        } catch (exception: Exception) {

            throw exception
        }
    }
}
What is the best approach to get the id of entity here?
b
T : IdentifiableObject
where
IdentifiableObject
has an ID property. But then all of your entities have to implement that interface.
n
Yeah I was trying to avoid that. Seems like I have to dig more into Spring code to figure out how to do this.
m
If you don’t want to implement an interface then you have to resort to reflection.
e
why do you want to avoid impl or extend member that forces having id property to children??? don't use reflection unless it can't be done by other methods