CHAPTER 14 Querying with HQL and JPA QL (Web design company)
CHAPTER 14 Querying with HQL and JPA QL FROM clause. Subselects in the SELECT clause are also not supported in the query language, but can be mapped to properties with a formula, as shown in Inverse joined properties in chapter 8, section 8.1.3. (Some platforms supported by Hibernate don t implement SQL subselects. Hibernate supports subselects only if the SQL database management system provides this feature.) Correlated and uncorrelated nesting The result of a subquery may contain either a single row or multiple rows. Typically, subqueries that return single rows perform aggregation. The following sub- query returns the total number of items sold by a user; the outer query returns all users who have sold more than 10 items: from User u where 10 < ( select count(i) from u.items i where i.successfulBid is not null ) This is a correlated subquery it refers to an alias (u) from the outer query The next subquery is an uncorrelated subquery: from Bid bid where bid.amount + 1 >= ( select max(b.amount) from Bid b ) The subquery in this example returns the maximum bid amount in the entire system; the outer query returns all bids whose amount is within one (dollar) of that amount. Note that in both cases, the subquery is enclosed in parentheses. This is always required. Uncorrelated subqueries are harmless, and there is no reason to not use them when convenient, although they can always be rewritten as two queries (they don t reference each other). You should think more carefully about the performance impact of correlated subqueries. On a mature database, the performance cost of a simple correlated subquery is similar to the cost of a join. However, it isn t necessarily possible to rewrite a correlated subquery using several separate queries. Quantification If a subquery returns multiple rows, it s combined with quantification. ANSI SQL, HQL, and JPA QL define the following quantifiers: ALL The expression evaluates to true if the comparison is true for all values in the result of the subquery. It evaluates to false if a single value of the subquery result fails the comparison test.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.