liveBook · Manning

Join

As explained in Java Persistence with Spring Data and Hibernate epub

A join is an SQL relational operation that combines rows from multiple tables according to a matching or other join condition.

A join combines rows from related tables so an application can retrieve the portion of an object network it expects to navigate with fewer database requests. Planning matters: joining too little causes piecemeal lazy loading, while joining too much wastes memory and can produce huge result sets. section 1.2.5

Use joins with these operational tradeoffs:

  • Join to-one associations efficiently; one query can retrieve an item, seller, address, and city without multiplying rows. section 12.2.2
  • Limit joined tables with fetch-depth configuration; beyond the limit, Hibernate continues eager loading through additional SELECT statements. section 12.2.2
  • Avoid eagerly joining multiple collections together because their rows can multiply into a Cartesian product. section 12.2.2

Filtering users through joined bids

Querydsl expresses the relationship and its join condition directly: queryFactory.selectFrom(QUser.user).innerJoin(QUser.user.bids, QBid.bid).on(QBid.bid.amount.eq(new BigDecimal("120.00"))).fetch(); Hibernate generates an INNER JOIN between User and Bid, restricting joined bids by amount. section 19.3.4

 item = em.find(Item.class, ITEM_ID);
// select i.*, u.*
//  from ITEM i
//   left outer join USERS u on u.ID = i.SELLER_ID
//  where i.ID = ?

Inspect the SQL shape of an ORM join that retrieves an Item and its seller in one SELECT. section 12.2.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains SQL joins, ORM fetch-depth limits, eager-loading behavior, Cartesian products, and Querydsl join predicates.
Examples
Uses User, BillingDetails, Item, User seller, Bid, and Image tables, plus Querydsl predicates and generated SQL.
What it assumes
Assumes related tables or mapped entity associations and predicates that define which rows should join.
What you can do after
Apply it via SQL, Hibernate fetch plans, or Querydsl to retrieve related entities and constrain joined rows.
How it connects
Connects lazy loading, eager fetching, fetch plans, object navigation, Cartesian products, subqueries, and Querydsl.

Also explained in

Compare how 16 books explain join →

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.