CHAPTER 14 Querying with HQL and JPA QL (Web hosting service)

CHAPTER 14 Querying with HQL and JPA QL You may also want to compare a foreign key value to a query parameter, perhaps to find all Comments from a User: User givenUser = … Query q = session.createQuery( “from Comment c where c.fromUser = :user” ); q.setEntity(”user”, givenUser); List result = q.list(); Alternatively, sometimes you prefer to express these kinds of queries in terms of identifier values rather than object references. An identifier value may be referred to by either the name of the identifier property (if there is one) or the special property name id. (Note that only HQL guarantees that id always refers to any arbitrarily named identifier property; JPA QL doesn t.) These queries are equivalent to the earlier queries: from Item i, User u where i.seller.id = u.id and u.username = ’steve’ from Item i, Bid b where i.seller.id = b.bidder.id However, you may now use the identifier value as a query parameter: Long userId = … Query q = session.createQuery( “from Comment c where c.fromUser.id = :userId” ); q.setLong(”userId”, userId); List result = q.list(); Considering identifier attributes, there is a world of difference between the following queries: from Bid b where b.item.id = 1 from Bid b where b.item.description like ‘%Foo%’ The second query uses an implicit table join; the first has no joins at all! This completes our discussion of queries that involve joins. You learned how to write a simple implicit inner join with dot notation and how to write an explicit inner or outer join with aliases in the FROM clause. We also looked at dynamic fetching strategies with outer and inner join SQL operations. Our next topic is advanced queries that we consider to be mostly useful for reporting.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Leave a Reply