206 CHAPTER 9 n SEARCHES AND QUERIES CHAPTER (Apache web server for windows)
Sunday, July 22nd, 2007206 CHAPTER 9 n SEARCHES AND QUERIES CHAPTER 9 n SEARCHES AND QUERIES join, right outer join, and full outer join. If you use cross join, just specify both classes in the from clause (from Product p, Supplier s). For the other joins, use a join clause after the from clause. Specify the type of join, the object property to join on, and an alias for the other class. You can use inner join to obtain the supplier for each product, and then retrieve the supplier name, product name, and product price, as so: select s.name, p.name, p.price from Product p inner join p.supplier as s You can retrieve the objects using similar syntax: from Product p inner join p.supplier as s We used aliases in these HQL statements to refer to the entities in our query expressions. These are particularly important in queries with associations that refer to two different entities with the same class for instance, if we are doing a join from a table back to itself. Commonly, these types of joins are used to organize tree data structures. Notice that Hibernate does not return Objectobjects in the result set; instead, Hibernate returns Objectarrays in the results. You will have to access the contents of the Objectarrays to get the Supplier and the Productobjects. If you would like to start optimizing performance, you can ask Hibernate to fetch the associated objects and collections for an object in one query. If you were using lazy loading with Hibernate, the objects in the collection would not be initialized until you accessed them. If you use fetch on a join in your query, you can ask Hibernate to retrieve the objects in the collection at the time the query executes. Add the fetchkeyword after the join in the query, like so: from Supplier s inner join fetch s.products as p When you use fetch for a query like this, Hibernate will return only the Supplier objects, not the Product objects. This is because you are specifying the join, so Hibernate knows which objects to fetch (instead of using lazy loading). If you need to get the Product objects, you can access them through the associated Supplierobject. You cannot use the properties of the Product objects in expressions in the where clause. Use of the fetch keyword overrides any settings you have in the mapping file for object initialization. Aggregate Methods HQL supports a range of aggregate methods, similar to SQL. They work the same way in HQL as in SQL, so you do not have to learn any specific Hibernate terminology. The difference is that in HQL, aggregate methods apply to the properties of persistent objects. The count(…) method returns the number of times the given column name appears in the result set. You may use the count(*) syntax to count all the objects in the result set, or count(product.name) to count the number of objects in the result set with a name property. Here is an example using the count(*) method to count all products: select count(*) from Product product
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.