Consider these beans:
class Country {
String name;
String code;
...
List<City> cities;
}
class City {
String name;
String zip;
...
List<Street> streets;
}
class Street {
String name;
}
I have to get that nested beans from 3 tables in a database.
I can solve in two ways:
Query loop (query on countries, looping results querying their citiy, looping results querying their streets....)
Full flat datasource (a single wide select joining all 3 tables with all rows at maximum details ordered by outer to inner fields) and after that split that.
The first question: is it the second solution the best choice considering that the nesting level can be deeper than 3 levels?
Let's say yes, I suppose to use the second option:
select *
from countries c
join cities t ...
join streets s ...
order by c.name, c.code, ..., t.name, t.zip, ...
The second question: how can I store that ResultSet
in beans with JdbcTemplate
?
Is there something for this purpose that split rows to nested beans? I cannot use a custom RowMapper
because I do not have a single outer bean for each row.