spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Introduce RowMapper with automatic binding to fields

Open membersound opened this issue 3 years ago • 5 comments

BeanPropertyRowMapper requires setters being present. Which is fine if you have some.

But if you only have a simple dto class for some reason, and want to make use of the mapper, it requires to create setters just for that.

It would be nice if we could have some DirectFieldRowMapper additionally, similar to: https://gist.github.com/stianl/6629122#file-directfieldrowmapper

Spring already allows direct field mapping for mvc with DataBinder.initDirectFieldAccess(). So why not providing a direct mapper also for jdbc beans, that can simply operate on public fields?

membersound avatar Feb 23 '21 15:02 membersound

Are you familiar with org.springframework.jdbc.core.DataClassRowMapper introduced in Spring Framework 5.3?

Does the "data class constructor with named parameters corresponding to column names" support meet your needs?

sbrannen avatar Feb 23 '21 15:02 sbrannen

No, as far as I understood the DataClassRowMapper only adds the ability to let the mapper create the object using a constructor. If you have a dto with public fields, you usually won't have a constructor matching all the fields.

Like, if the db row has 20 columns, you would have to introduce a 20 column constructor to make it work. This is no advantage over creating 20 setters.

membersound avatar Feb 23 '21 16:02 membersound

Like, if the db row has 20 columns, you would have to introduce a 20 column constructor to make it work. This is no advantage over creating 20 setters.

Sure. There's no real benefit in that.

sbrannen avatar Feb 23 '21 16:02 sbrannen

We've assigned this proposal to the 5.x Backlog for potential inclusion in 5.3.x

sbrannen avatar Feb 23 '21 16:02 sbrannen

Would still be great if that feature could get some investigation.

membersound avatar Oct 06 '22 07:10 membersound