liveBook · Manning

Cartesian product

As explained in Java Persistence with Spring Data and Hibernate epub

A relational operation that forms every possible pairing of rows from two relations, potentially producing redundant combinations when multiple collections are joined.

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

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains row multiplication, redundant data, and operational costs, but does not detail database execution optimization.
Examples
Uses an Item with bids and images, showing nine rows for 3 × 3 collections and 250 rows for 50 × 5 collections.
What it assumes
Assumes basic SQL and ORM familiarity while reviewing restrictions, projections, JOINs, and Cartesian products.
What you can do after
Diagnose redundant joined results and select a procedure-specific fetching strategy instead of eagerly joining multiple collections.
How it connects
Connects Cartesian products with eager loading, lazy loading, SQL JOINs, fetch strategies, and the N+1 selects problem.

Also explained in

Compare how 2 books explain Cartesian product →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.