A Cartesian product pairs every row from one relation with every row from another. In ORM fetching, it becomes a performance problem when one SQL query eagerly joins multiple collections, repeating parent and collection data across the result set. section 1.1.2 section 12.2.2
Recognize the product by its fetching pattern and operational cost:
- Eagerly joining two collections multiplies their sizes; three bids and three images produce 3 × 3 = 9 result rows. section 12.2.2
- Larger collections scale rapidly: 50 bids and 5 images can produce 250 rows for one item. section 12.2.2
- Redundant rows consume database processing time, memory, and network bandwidth, even though Hibernate uses only relevant cells to build collections. section 12.2.2
- Avoid treating eager loading of multiple collections as a general solution; choose a fetching strategy for each procedure and use case. section 12.2 section 12.2.2
Eagerly joining bids and images
An Item with eager bids and images may generate `select i.*, b.*, img.* from ITEM i left outer join BID b on b.ITEM_ID = i.ID left outer join IMAGE img on img.ITEM_ID = i.ID`. With three bids and three images, the database returns nine rows, while the detached entity still exposes three bids and three images. section 12.2.2
Path: Ch12/cartesianproduct/src/test/java/com/manning/javapersistence/ch12 /cartesianproduct/CartesianProduct.java Item item = em.find(Item.class, ITEM_ID); // select i.*, b.*, img.* // from ITEM i // left outer join BID b on b.ITEM_ID = i.ID // left outer join IMAGE img on img.ITEM_ID = i.ID // where i.ID = ? em.detach(item); assertEquals(3, item.getImages().size(); assertEquals(3, item.getBids().size());
Inspect the Java entity lookup and SQL JOIN that eagerly loads the Item, bids, and images collections. section 12.2.2

