CHAPTER 14 Querying with (Web site design) HQL and JPA QL
Monday, April 21st, 2008CHAPTER 14 Querying with HQL and JPA QL Expressions with collections All expressions in the previous sections included only single-valued path expressions: user.email, bid.amount, and so on. You can also use path expressions that end in collections in the WHERE clause of a query, with the right operators. For example, let s assume you want to restrict your query result by the size of a collection: from Item i where i.bids is not empty This query returns all Item instances that have an element in their bids collection. You can also express that you require a particular element to be present in a collection: from Item i, Category c where i.id = ‘123′ and i member of c.items This query returns Item and Category instances usually you add a SELECT clause and project only one of the two entity types. It returns an Item instance with the primary key ‘123′ (a literal in single quotes) and all Category instances this Item instance is associated with. (Another trick you use here is the special .id path; this field always refers to the database identifier of an entity, no matter what the name of the identifier property is.) There are many other ways to work with collections in HQL and JPA QL. For example, you can use them in function calls. Calling functions An extremely powerful feature of HQL is the ability to call SQL functions in the WHERE clause. If your database supports user-defined functions (most do), you can put this to all sorts of uses, good or evil. For the moment, let s consider the usefulness of the standard ANSI SQL functions UPPER() and LOWER(). These can be used for case-insensitive searching: from User u where lower(u.email) = ‘foo@hibernate.org’ Another common expression is concatenation although SQL dialects are different here, HQL and JPA QL support a portable concat() function: from User user where concat(user.firstname, user.lastname) like ‘G% K%’ Also typical is an expression that requires the size of a collection: from Item i where size(i.bids) > 3 JPA QL standardizes the most common functions, as summarized in table 14.2.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.