Skip to content

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

Closed as not planned

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.

Activity

  1. mp911de commented on Jan 9, 2023

    @mp911de
    Member

    Composite Identifiers (that is what this boils down to) are not yet supported.

  2. GeorgePap-719 commented on Jan 9, 2023

    @GeorgePap-719
    ContributorAuthor

    Thank you for the answer. I'm not familiar with composite identifiers, is there any issue/link/doc for this (i searched the issues and could not find one)?

  3. mp911de commented on Jan 9, 2023

    @mp911de
    Member

    You can follow #1276 for the composite primary key feature. Wrapping the primary key value in a different type creates a composite key type because the identifier type no longer matches a supported primitive type.

  4. mp911de commented on Jan 9, 2023

    @mp911de
    Member

    Closing as a duplicate for the time being.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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