krush icon indicating copy to clipboard operation
krush copied to clipboard

AttributeConverter only supports wrapped String/Long

Open tblakers opened this issue 5 years ago • 2 comments

My domain model contains inline classes wrapping UUIDs. I'd like to be able to store these using the native Postgres UUID type, and this is allowed by JPA AttributeConverter class. Unfortunately the processor requires the target type to be either a Long or a String.

    private fun converterFunc(name: String, type: TypeName, it: ConverterDefinition, fileSpec: FileSpec.Builder) {
        val wrapperName = when (it.targetType.asClassName()) {
            STRING -> "stringWrapper"
            LONG -> "longWrapper"
            else -> throw TypeConverterNotSupportedException(it.targetType)
        }

Is there any particular reason for this, and if not would you accept a PR that adds UUID as an option?

tblakers avatar Aug 11 '20 01:08 tblakers

Yes, for now we only support Long ang String, having UUID sounds like a resonable idea. Do you have any converter code that transforms your classes into UUIDColumnType?

pjagielski avatar Aug 12 '20 10:08 pjagielski

In Postgres at least, UUID is a native type, so a converter is very simple:

data class MyUUID(id: UUID)

class MyUUIDConverter : AttributeConverter<MyUUID, UUID> {
    override fun convertToDatabaseColumn(attribute: MyUUID): UUID {
        return attribute.id
    }

    override fun convertToEntityAttribute(dbData: UUID): MyUUID {
        return MyUUID(dbData)
    }
}

tblakers avatar Aug 17 '20 00:08 tblakers