CHAPTER 15 Advanced query options (Web design templates) ” where b.ITEM_ID

CHAPTER 15 Advanced query options ” where b.ITEM_ID = {alias}.ITEM_ID )” ) ); This query returns all Item objects which have no bids greater than 100. (The Hibernate criteria query system is extensible: You could also wrap the LENGTH() SQL function in your own implementation of the Criterion interface.) Finally, you can write criteria queries that include subqueries. Writing subqueries A subquery in a criteria query is a WHERE clause subselect. Just like in HQL, JPA QL, and SQL, the result of a subquery may contain either a single row or multiple rows. Typically, subqueries that return single rows perform aggregation. The following subquery returns the total number of items sold by a user; the outer query returns all users who have sold more than 10 items: DetachedCriteria subquery = DetachedCriteria.forClass(Item.class, “i”); subquery.add( Restrictions.eqProperty(”i.seller.id”, “u.id”)) .add( Restrictions.isNotNull(”i.successfulBid”) ) .setProjection( Property.forName(”i.id”).count() ); Criteria criteria = session.createCriteria(User.class, “u”) .add( Subqueries.lt(10, subquery) ); This is a correlated subquery. The DetachedCriteria refers to the u alias; this alias is declared in the outer query. Note that the outer query uses a less than operator because the subquery is the right operand. Also note that i.seller.id does not result in a join, because SELLER_ID is a column in the ITEM table, which is the root entity for that detached criteria. Let s move on to the next topic about criteria queries: joins and dynamic fetching. 15.1.2 Joins and dynamic fetching Just like in HQL and JPA QL, you may have different reasons why you want to express a join. First, you may want to use a join to restrict the result by some property of a joined class. For example, you may want to retrieve all Item instances that are sold by a particular User. Of course, you also want to use joins to dynamically fetch associated objects or collections, as you d do with the fetch keyword in HQL and JPA QL. In criteria queries you have the same options available, with a FetchMode.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Leave a Reply