model object to be inserted into a database. I want to insert these into various databases using a Repository pattern, but the
User
has an
id
field that is generated differently for each database I use. Is there a way to defer the definition of the
id
type and a
generateId()
function until the actual creation of the
User
object in Kotlin? Thanks
g
gildor
10/13/2022, 4:14 AM
No, it’s not possible direclty, but you alway can create a new instance of User object with new database id, and maybe on insert make id nullable (or some default value)
Don’t say that it’s the best, but in general ORM approach is bad IMO and such issues as one you described haunt apps with ORMs forever
c
Casey Brooks
10/13/2022, 4:37 AM
If you're able to change the record schema, I've recently been favoring UUIDs created and assigned in the application code instead of relying on anything generated by the database/ORM. It makes situations like this so much easier to work with. Java's got a built-in UUID class, or this library works well for multiplatform https://github.com/benasher44/uuid
➕ 1
j
Justin Xu
10/13/2022, 4:54 AM
Thanks for the feedback 😄 Nice to see I'm not the only one with headache from this issue. I'll look into UUID and other ways to avoid using ORM
g
gildor
10/13/2022, 6:09 AM
Uuid is not the best solution for indexes, but depends on your scale