Jetty web server - CHAPTER 14 Querying with HQL and JPA QL

CHAPTER 14 Querying with HQL and JPA QL and projection. The only thing that will make it more efficient is the right dynamic fetching strategy. For example, there is no reason why you need several SQL statements to fetch all Item instances and to initialize their bids collections, or to retrieve the seller for each Item. This can be done at the same time, with a join operation. In HQL and JPA QL you can specify that an associated entity instance or a collection should be eagerly fetched with the FETCH keyword in the FROM clause: from Item i left join fetch i.bids where i.description like ‘%Foo%’ This query returns all items with a description that contains the string “Foo” and all their bids collections in a single SQL operation. When executed, it returns a list of Item instances, with their bids collections fully initialized. This is quite different if you compare it to the ordered pairs returned by the queries in the previous section! The purpose of a fetch join is performance optimization: You use this syntax only because you want eager initialization of the bids collections in a single SQL operation: select i.DESCRIPTION, i.INITIAL_PRICE, … b.BID_ID, b.AMOUNT, b.ITEM_ID, b.CREATED_ON from ITEM i left outer join BID b on i.ITEM_ID = b.ITEM_ID where i.DESCRIPTION like ‘%Foo%’ An additional WITH clause wouldn t make sense here. You can t restrict the Bid instances: All the collections must be fully initialized. You can also prefetch many-to-one or one-to-one associations, using the same syntax: from Bid bid left join fetch bid.item left join fetch bid.bidder where bid.amount > 100 This query executes the following SQL: select b.BID_ID, b.AMOUNT, b.ITEM_ID, b.CREATED_ON i.DESCRIPTION, i.INITIAL_PRICE, … u.USERNAME, u.FIRSTNAME, u.LASTNAME, … from BID b left outer join ITEM i on i.ITEM_ID = b.ITEM_ID left outer join USER u on u.USER_ID = b.BIDDER_ID where b.AMOUNT > 100
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply