220 CHAPTER 10 n ADVANCED QUERIES (Web site templates) USING CRITERIA
220 CHAPTER 10 n ADVANCED QUERIES USING CRITERIA CHAPTER 10 n ADVANCED QUERIES USING CRITERIA To use projections, start by getting the org.hibernate.criterion.Projection object you need from the org.hibernate.criterion.Projections factory class. The Projectionsclass is similar to the Restrictions class in that it provides several static factory methods for obtaining Projection instances. After you get a Projectionobject, add it to your Criteria object with the setProjection()method. When the Criteria object executes, the list contains object references that you can cast to the appropriate type. The row-counting functionality provides a simple example of applying projections. The code looks similar to the restrictions examples we were working with earlier in the chapter: Criteria crit = session.createCriteria(Product.class); crit.setProjection(Projections.rowCount()); List results = crit.list(); The results list will contain one object, an Integer that contains the results of executing the COUNTSQL statement. Other aggregate functions available through the Projections factory class include the following: avg(String propertyName): Gives the average of a property s value count(String propertyName): Counts the number of times a property occurs countDistinct(String propertyName): Counts the number of unique values the property contains max(String propertyName): Calculates the maximum value of the property values min(String propertyName): Calculates the minimum value of the property values sum(String propertyName): Calculates the sum total of the property values We can apply more than one projection to a given Criteria object. To add multiple projections, get a projection list from the projectionList()method on the Projections class. The org.hibernate.criterion.ProjectionList object has an add() method that takes a Projection object. You can pass the projections list to the setProjection() method on the Criteria object because ProjectionList implements the Projection interface. The following example demonstrates some of the aggregate functions, along with the projection list: Criteria crit = session.createCriteria(Product.class); ProjectionList projList = Projections.projectionList(); projList.add(Projections.max(”price”)); projList.add(Projections.min(”price”)); projList.add(Projections.avg(”price”)); projList.add(Projections.countDistinct(”description”)); crit.setProjection(projList); List results = crit.list(); When you execute multiple aggregate projections, you get a List with an Object array as the first element. The Object array contains all of your values, in order. Another use of projections is to retrieve individual properties, rather than entities. For instance, we can retrieve just the name and description from our product table, instead of faulting the classes into memory. Use the property() method on the Projections class to
You want to have a cheap webhost for your apache application, then check apache web hosting services.