Yanming Zhou opened SPR-15004 and commented
I'm writing my own RowMapper extends BeanPropertyRowMapper to supports nested property, for example:
public class Person{
private Name name;
//getter and setter;
}
public class Name{
private String firstName;
private String lastName;
//getter and setter
}
select firstName as "name.firstName", lastName as "name.lastName" from person
I wish BeanPropertyRowMapper can expose such methods for overrides
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
......
boolean fromFind = false;
if (pd == null) {
pd = tryFindPropertyDescriptor(column, bw);
fromFind = true;
}
......
try {
if (fromFind) {
bw.setPropertyValue(column, value);
} else {
bw.setPropertyValue(pd.getName(), value);
}
} catch (TypeMismatchException ex) {
......
}
......
}
protected PropertyDescriptor tryFindPropertyDescriptor(String column, BeanWrapper bw) {
return null;
}
Subclass can override those methods:
@Override
protected PropertyDescriptor tryFindPropertyDescriptor(String column, BeanWrapper bw) {
if (column.indexOf('.') > 0)
return bw.getPropertyDescriptor(column);
return null;
}
And there is a fallback options, change field visibility of BeanPropertyRowMapper from private to protected, let subclass override mapRow entirely, I prefer to the first solution.
Affects: 4.3.4
Comment From: jhoeller
On review, BeanPropertyRowMapper
is very tied to its initialization approach where it introspects the available bean properties, adapting their name, possibly validating them whether an instance is fully populated etc.
Custom nested property support rather feels like a distinct RowMapper
implementation to me, sharing some characteristics with BeanPropertyRowMapper.mapRow
but not really fitting as a subclass of it.
Also, since we had to open up BeanPropertyRowMapper
for the introduction of DataClassRowMapper
already, it serves multiple purposes and arguably has too many extension points already.