https://kotlinlang.org logo
#komapper
Title
# komapper
s

S.

11/25/2023, 8:06 PM
I'm using the gradle plugin to generate entities and definitions. however UUID is getting mapped to String and timestamp or date are being mapped to java.time.* instead of kotlinx.datetime.* even with defining the komapper datetime dependency. is there anything I need to configure or do I have to add some DataTypeConverters to specify the types correctly?
I can manually replace these types afterwards and everything else works as expected
t

Toshihiro Nakamura

11/26/2023, 7:18 AM
You can use
org.komapper.codegen.PropertyTypeResolver
. Below is an example of a build.gradle.kts that specifies a UUID for the ID property using
PropertyTypeResolver
.
Copy code
komapper {
    generators {
        val basePackage = "org.komapper.example"
        register("postgresql") {
            jdbc {
                val initScript = file("src/main/resources/init_postgresql.sql")
                driver.set("org.testcontainers.jdbc.ContainerDatabaseDriver")
                url.set("jdbc:tc:postgresql:13.3:///test?TC_INITSCRIPT=file:${initScript.absolutePath}")
                user.set("test")
                password.set("test")
            }
            packageName.set("$basePackage.postgres")
            overwriteEntities.set(true)
            overwriteDefinitions.set(true)
            val defaultResolver = propertyTypeResolver.get()
            val newResolver = object : PropertyTypeResolver by defaultResolver {
                override fun resolve(table: Table, column: Column): String {
                    if (column.name.lowercase() == "id" || column.name.lowercase().endsWith("_id")) {
                        return "java.util.UUID"
                    }
                    return defaultResolver.resolve(table, column)
                }
            }
            propertyTypeResolver.set(newResolver)
        }
    }
}
s

S.

11/26/2023, 12:17 PM
ah yes, that works. thank you
👍 1
5 Views