Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GeorgePap-719 opened this issue Jan 9, 2023 · 4 comments
Labels
status: duplicate A duplicate of another issue

Comments

@GeorgePap-719
Copy link
Contributor

GeorgePap-719 commented Jan 9, 2023

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 9, 2023
@mp911de
Copy link
Member

mp911de commented Jan 9, 2023

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

@GeorgePap-719
Copy link
Contributor Author

GeorgePap-719 commented Jan 9, 2023

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)?

@mp911de
Copy link
Member

mp911de commented Jan 9, 2023

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.

@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 9, 2023
@mp911de
Copy link
Member

mp911de commented Jan 9, 2023

Closing as a duplicate for the time being.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants