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

BeanPropertyRowMapper's support for direct column name matches is missing in DataClassRowMapper #28243

Closed
madorb opened this issue Mar 28, 2022 · 3 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug
Milestone

Comments

@madorb
Copy link

madorb commented Mar 28, 2022

Presently DataClassRowMapper appears to assume that all database columns use snake_case

Say I have a kotlin class like:

data class MyClass(val myFoo: String, val myBar: String)

and a database table with columns myFoo, myBar

a naive selection like

select myFoo, myBar
FROM ...

will fail, because the mapper will expect the columns to be named with underscores.

This can be worked around by adding aliases with underscores:

select myFoo as my_foo, myBar as my_bar
FROM ...

But this is less than ideal. Ideally this would be configurable and support a few standard formats out of the box, e.g. snake_case, camelCase, PascalCase.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 28, 2022
@sbrannen
Copy link
Member

Presently DataClassRowMapper appears to assume that all database columns use snake_case

This is by design.

DataClassRowMapper extends BeanPropertyRowMapper, and the class-level Javadoc for BeanPropertyRowMapper states:

Column values are mapped based on matching the column name as obtained from result set meta-data to public setters for the corresponding properties. The names are matched either directly or by transforming a name separating the parts with underscores to the same name using "camel" case.

...

To facilitate mapping between columns and fields that don't have matching names, try using column aliases in the SQL statement like select fname as first_name from customer.

...

Please note that this class is designed to provide convenience rather than high performance. For best performance, consider using a custom RowMapper implementation.

@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Mar 29, 2022
@jhoeller
Copy link
Contributor

There is indeed a design convention for either the lower-cased name matching directly or the underscored name matching. However, this seems to be inconsistently implemented for constructor argument resolution in DataClassRowMapper where only the underscored naming is actually applied. I'll align this for 5.3.18.

@jhoeller jhoeller changed the title Support column formats other than underscored in DataClassRowMapper BeanPropertyRowMapper's support for direct column name matches is missing in DataClassRowMapper Mar 29, 2022
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 29, 2022
@jhoeller jhoeller added this to the 5.3.18 milestone Mar 29, 2022
@jhoeller jhoeller self-assigned this Mar 29, 2022
@madorb
Copy link
Author

madorb commented Mar 29, 2022

Thanks @jhoeller - you mention "lower-cased" name matching directly, which is slightly different than what the documentation says The names are matched either directly or by...

if i have a class property and database column with exactly matching, but non-lowercase, names e.g. in my example above, would that still not be supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants