Selecting a fetch strategy Item item = (Item) (Web page design)
Thursday, January 31st, 2008Selecting a fetch strategy Item item = (Item) session.get(Item.class, new Long(123)); This operation triggers the following SQL SELECT: select i.*, u.* from ITEM i left outer join USERS u on i.SELLER_ID = u.USER_ID where i.ITEM_ID = ? Obviously, the seller is no longer lazily loaded on demand, but immediately. Hence, a fetch=”join” disables lazy loading. If you only enable eager fetching with lazy=”false”, you see an immediate second SELECT. With fetch=”join”, you get the seller loaded in the same single SELECT. Look at the resultset from this query shown in figure 13.4. Figure 13.4 Two tables are joined to eagerly fetch associated rows. Hibernate reads this row and marshals two objects from the result. It connects them with a reference from Item to User, the seller association. If an Item doesn t have a seller all u.* columns are filled with NULL. This is why Hibernate uses an outer join, so it can retrieve not only Item objects with sellers, but all of them. But you know that an Item has to have a seller in CaveatEmptor. If you enable
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.