Skip to content

R2dbcConverter should provide read functionality that accepts alias prefixes for columns #448

Open
@pmaedel

Description

@pmaedel

There is an example project available here

I used the r2dbc mapping functionality to map joined entities via alias prefixes:

    override suspend fun findByIdJoined(id: Long): Fruit? {

        return databaseClient.execute("select f.*, t.id as t_id, t.kind as t_kind from fruit f left join tree t on t.id = f.tree_id where f.id = :id")
            .bind("id", id)
            .map { row, rowMetaData ->
                val fruitEntity = converter.read(Fruit::class.java, row, rowMetaData)
                val joinedTreeEntity = converter.read<Tree>(row, "t_")
                fruitEntity.copy(joinedTree = joinedTreeEntity)
            }.awaitFirstOrNull()
    }

    private inline fun <reified T> R2dbcConverter.read(row: Row, aliasPrefix: String): T = read(T::class.java,
        object : Row {
            override fun <T : Any?> get(index: Int, type: Class<T>) = row[index, type]
            override fun <T : Any?> get(name: String, type: Class<T>) = row["$aliasPrefix$name", type]
        }
    )

I can easily map the root entity (tree) via read(type, row, rowMetadata). Mapping the aliased joined entity (fruit) however seems only to be possible by hacking a customized read functionality, which would better be provided via R2dbcConverter. I have seen that functionality for reading values with column prefixes is available internally via MappingR2dbcConverter#readFrom but could not find a way to make use of it for my use case.

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