Hi, i have encountered a weird problem. I have a s...
# spring
g
Hi, i have encountered a weird problem. I have a simple entity and im saving this entity in db with r2dbc using
R2dbcEntityTemplate
directly :
Copy code
class MessageRepositoryImpl(private val template: R2dbcEntityTemplate) : MessageRepository {
    override suspend fun save(messageEntity: MessageEntity): MessageEntity =
        template.insert<MessageEntity>().usingAndAwait(messageEntity)
}
So far so good. now i want to refactor my entity's id to be a data class. for example: before:
Copy code
@Table("messages")
data class MessageEntity(
    @Id
    @Column("id")
    val id: Int = 0,
}
after:
Copy code
data class MessageId(private val value: Int) {
    fun toInt() = value
    override fun toString(): String = value.toString()
}

@Table("messages")
data class MessageEntity(
    @Id
    @Column("id")
    val id: MessageId = MessageId(0)
}
Now whenever i save something with above
save()
function it does not return the write result like before (i.e. the id which is on purpose 0 does not get returned with the actual value). But the entity is saved properly in db with proper (auto-increased) id, meaning if i search for it the result is correct. Has anyone encountered any similar problem? Thanks in advance for any help !! (More info in thread)
Note: i have custom @WritingConverter & @ReadingConverter for this entity:
Copy code
@Component
@WritingConverter
class MessageToRowConverter : Converter<MessageEntity, OutboundRow> {
    override fun convert(source: MessageEntity): OutboundRow {
        return OutboundRow()
            .append("id", source.id.toInt())
    }
}

@Component
@ReadingConverter
class RowToMessageConverter : Converter<Row, MessageEntity> {
    override fun convert(source: Row): MessageEntity {
        println("converting from row")
        return MessageEntity(
            id = MessageId(source.getColumn("id"))
        )
    }
}
Note 2: During my search on web i found this two similar issues: https://github.com/spring-projects/spring-data-r2dbc/issues/49, https://github.com/spring-projects/spring-data-r2dbc/issues/218. I think my case is different from them since my entity is actual saved in db properly and with proper id.