1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tobia
  • 9,165
  • 28
  • 114
  • 219

1 Answers1

0

Performance-wise, the first solution is really bad. This means that you'll run a lot of queries against your database, and the number of these queries really depends on number of rows.

I believe the database just won't be able to handle that eventually.

The second approach is much better, you can handle it in one query and since you use jdbc template, creating the corresponding objects must be not hard.

If you still have a lot of data, you might want to consider retrieving with paging (bring 100 records, than bring yet another 100 records).

Most of the DB drivers already do this in one way or another, you might want to add an "application level" handling for this if you don't want to keep in memory all the data at once, but this really depends on use cases.

If the tables a really big though joins can also be costly, but in this case the first approach will fail in any case (just too many queries because the data size is big).

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Thank you Mark, but how to "split" the flat resultset to nested beans? Does spring jdbctemplate implements something for this pattern? – Tobia Feb 18 '16 at 08:08
  • I just mentioned differenced between approaches to execute a query. For you question: please read the answer here (the accepted one): http://stackoverflow.com/questions/16718163/jdbctemplate-set-nested-pojo-with-beanpropertyrowmapper – Mark Bramnik Feb 18 '16 at 08:10
  • Very interesting the "autogrow" setting, but in my case I cannot understand how to use a rowmapper because if I have 100rows I do not expect 100 outer beans... – Tobia Feb 18 '16 at 08:16
  • I try to better explain my problem with that solution. If I have 3 rows for Italy country with 3 different cities I want ONLY ONE "Italy" bean with inside 3 cities and not 3 different "Italy" beans. As far as I know rowmapper maps one object per row. – Tobia Feb 18 '16 at 08:32