A fetch plan specifies which associations and collections are loaded, and when. The global default is defined in entity mappings with FetchType.LAZY and FetchType.EAGER; individual operations can use a different plan. A fetch strategy determines how the data is loaded, such as by a SQL join or separate selects. section 12.1 section 12.4
A practical default is to map associations and collections lazily, then load what each use case needs. Hibernate uses entity proxies and collection wrappers to load data on demand while the persistence context is open. Eager loading is a requirement; lazy loading is a hint the provider may ignore. section 12.1 section 12.1.3
Loading data before detachment
If an Item’s seller and bids are eagerly loaded, they remain accessible after the Item is detached. An uninitialized bidder proxy within a bid is still unavailable: eager loading the Item’s immediate associations does not automatically initialize the entire graph. Always loading bids can also waste work when an operation only needs the Item’s name. section 12.1.3
A lazy default can produce the n+1 selects problem: one query loads the Items, and accessing each seller can trigger another query. Making everything eager is not a universal fix; loading several collections can produce a large Cartesian product. Choose a strategy for the access pattern and inspect both statement count and result size. section 12.2.1 section 12.2
Path: Ch12/nplusoneselects/src/test/java/com/manning/javapersistence/ch12 /nplusoneselects/NPlusOneSelects.java List<Item> items = em.createQuery("select i from Item i").getResultList(); // select * from ITEM for (Item item : items) { assertNotNull(item.getSeller().getUsername()); #1 // select * from USERS where ID = ? } #1 Whenever we access a seller, each of them must be loaded with an additional <kbd>SELECT</kbd>.
This loop demonstrates how navigating from loaded Items to their sellers can issue additional selects. section 12.2.1
Use-case-specific options include:
- Entity graphs declare attributes and associations to load for find or query operations. A load graph treats listed attributes as eager and leaves unlisted attributes governed by their mappings. Graphs can be named or built with an API, and can include subgraphs. section 12.3.2
- Hibernate fetch profiles selectively override lazy mappings with join fetching for a unit of work. Unlike a graph, a profile combines a plan with a fetching strategy. section 12.3 section 12.4
- Batch or subselect prefetching can reduce queries while retaining a lazy default. Subselect prefetching depends on the original query remembered in the current persistence context. section 12.2.4
Path: Ch12/fetchloadgraph/src/test/java/com/manning/javapersistence/ch12 /fetchloadgraph/FetchLoadGraph.java EntityGraph<Item> itemGraph = em.createEntityGraph(Item.class); itemGraph.addAttributeNodes(Item_.seller); #1 Map<String, Object> properties = new HashMap<>(); properties.put("javax.persistence.loadgraph", itemGraph); Item item = em.find(Item.class, ITEM_ID, properties); // select i.*, u.* // from ITEM i // inner join USERS u on u.ID = i.SELLER_ID // where i.ID = ? #1 The <kbd>Item_</kbd> class belongs to the static metamodel. It is automatically generated by including the Hibernate JPA2 Metamodel Generator dependency in the project. Take a look back at section 3.3.4 for more details.
This API-built load graph requests the seller when finding an Item; other attributes retain their mapped fetch behavior. section 12.3.2
