|
Some SQL vendors offer the MULTISET type that can be used to return subqueries in the projection.
SELECT
ani_id,
ani_name,
ani_size,
MULTISET( SELECT loc_name, loc_description, loc_id FROM Location WHERE loc_id=ani_loc_fk ) ani_locs
FROM Animal
WHERE ani_id = 23
In particular, it prevents the cartesian issue of loading more than one bag semantic collections per query. It would also massively reduce the number of resultset lines to process in memory when multiple collections are returned as there would be no line explosions due to the cartesian product.
According to Lukas, Informix and CUBRID would support that syntax. Oracle does as well but requires to define a custom table type for the nested query in the MULTISET (not as convenient for the user). Maybe PostgreSQL could simulate that via array_agg() + ROW().
This could either be a dialect driven option, or another specific fetch mode akin to SUBSELECT.
Some of the background in in this Twitter conversation https://twitter.com/Hibernate/status/549925070190424064
|