Web hosting domains - CHAPTER 14 Querying with HQL and JPA QL
Wednesday, April 30th, 2008CHAPTER 14 Querying with HQL and JPA QL Instead of a List of Items, this query returns a List of Object[] arrays. At index 0 is the Item, and at index 1 is the Bid. A particular Item may appear multiple times, once for each associated Bid. These duplicate items are duplicate in-memory references, not duplicate instances! If you don t want the Bids in the query result, you may specify a SELECT clause in HQL (it s mandatory anyway for JPA QL). You use the alias in a SELECT clause to project only the objects you want: select i from Item i join i.bids b where i.description like ‘%Foo%’ and b.amount > 100 Now the generated SQL looks like this: select i.DESCRIPTION, i.INITIAL_PRICE, … from ITEM i inner join BID b on i.ITEM_ID = b.ITEM_ID where i.DESCRIPTION like ‘%Foo%’ and b.AMOUNT > 100 The query result contains just Items, and because it s an inner join, only Items that have Bids: Query q = session.createQuery(”select i from Item i join i.bids b”); Iterator items = q.list().iterator(); while ( items.hasNext() ) { Item item = (Item) items.next(); } As you can see, using aliases in HQL and JPA QL is the same for both direct classes and joined associations. You used a collection in the previous examples, but the syntax and semantics are the same for single-valued associations, such as many-toone and one-to-one. You assign aliases in the FROM clause by naming the association and then use the aliases in the WHERE and possibly SELECT clause. HQL and JPA QL offer an alternative syntax for joining a collection in the FROM clause and to assign it an alias. This IN() operator has its history in an older version of EJB QL. It s semantics are the same as those of a regular collection join. You can rewrite the last query as follows: select i from Item i in(i.bids) b where i.description like ‘%Foo%’ and b.amount > 100 The from Item i in(i.bids) b results in the same inner join as the earlier example with fromItemijoini.bids b.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.