George
01/05/2023, 2:07 PMR2dbcEntityTemplate
directly :
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:
@Table("messages")
data class MessageEntity(
@Id
@Column("id")
val id: Int = 0,
}
after:
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)George
01/05/2023, 2:09 PM@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"))
)
}
}
George
01/05/2023, 2:12 PM