spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.75k stars 38.15k forks source link

Treat numbers as separate elements in `JdbcUtils.convertPropertyNameToUnderscoreName` #33828

Open brieuc-le-faucheur opened 3 weeks ago

brieuc-le-faucheur commented 3 weeks ago

Hello!

While using JdbcClient, I have encountered a specific case while querying a column containing a digit. The mapping method JdbcUtils.convertPropertyNameToUnderscoreName does not handle numbers as separate words.

Using PostgreSQL, given a table declared such as this:

create table demo (
    id text,
    name_with_1_number_in_label text
);

Querying it with a data class declared as below will result in a PSQLException:

data class DemoModel(val id: String, val nameWith1NumberInLabel: String)

@Repository
class SimpleRepository(private val jdbcClient: JdbcClient) {

    fun getBy(id: String): MutableList<DemoModel> {

        return jdbcClient.sql("""
            select * from demo where id = :id
        """.trimIndent())
            .param("id", id)
            .query(DemoModel::class.java)
            .list()
    }
}

I think isolating numbers with underscore would match naming usage more than having numbers stuck to the word preceding it in the column name.

Here is a sample project demonstrating the exception along with the PR.

numbersInJdbcColumnNames.zip

pivotal-cla commented 3 weeks ago

@brieuc-le-faucheur Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

pivotal-cla commented 3 weeks ago

@brieuc-le-faucheur Thank you for signing the Contributor License Agreement!

sbrannen commented 3 weeks ago

Please note that the build failed for your PR.

FAILURE: Build failed with an exception.
294 actionable tasks: 139 executed, 155 from cache

* What went wrong:
Execution failed for task ':spring-jdbc:checkstyleMain'.
> A failure occurred while executing org.gradle.api.plugins.quality.internal.CheckstyleAction
   > Checkstyle rule violations were found. See the report at: file:///home/runner/work/spring-framework/spring-framework/spring-jdbc/build/reports/checkstyle/main.html
     Checkstyle files with violations: 1
     Checkstyle violations by severity: [error:2]

Please make sure you run a full local build before submitting a PR.

Thanks

brieuc-le-faucheur commented 2 weeks ago

Hello @sbrannen ! I updated the PR so that the full build passes. Sorry for the inconvenience.