Skip to content

r2dbc: insert() operation does not work properly when the id is wrapped in a data class #1407

Closed as not planned
@GeorgePap-719

Description

@GeorgePap-719

Hi, i have encountered a problem. I have a simple entity and im saving this entity in db with r2dbc using R2dbcEntityTemplate 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.

Additional info

Note: i have custom @WritingConverter & @ReadingConverter for this entity:

@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:
I searched through other issues and i found spring-projects/spring-data-r2dbc#49 and #218. I think my case is different from them since my entity is actual saved in db properly and with proper id.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions