CHAPTER 15 (Web server iis) Advanced query options 15.1.3 Projection and
CHAPTER 15 Advanced query options 15.1.3 Projection and report queries In HQL, JPA QL, and SQL, you write a SELECT clause to define the projection for a particular query. The Criteria API also supports projection, of course programmatically and not string-based. You can select exactly which objects or properties of objects you need in the query result and how you possibly want to aggregate and group results for a report. Simple projection lists The following criteria query returns only the identifier values of Item instances which are still on auction: session.createCriteria(Item.class) .add( Restrictions.gt(”endDate”, new Date()) ) .setProjection( Projections.id() ); The setProjection() method on a Criteria accepts either a single projected attribute, as in the previous example, or a list of several properties that are to be included in the result: session.createCriteria(Item.class) .setProjection( Projections.projectionList() .add( Projections.id() ) .add( Projections.property(”description”) ) .add( Projections.property(”initialPrice”) ) ); This query returns a List of Object[], just like HQL or JPA QL would with an equivalent SELECT clause. An alternative way to specify a property for projection is the Property class: session.createCriteria(Item.class) .setProjection( Projections.projectionList() .add( Property.forName(”id”) ) .add( Property.forName(”description”) ) .add( Property.forName(”initialPrice”) ) ); In HQL and JPA QL, you can use dynamic instantiation with the SELECT NEW operation and return a collection of custom objects instead of Object[]. Hibernate bundles a ResultTransformer for criteria queries that can do almost the same (in fact, it s more flexible). The following query returns the same result as the previous one, but wrapped in data transfer objects: session.createCriteria(Item.class) .setProjection( Projections.projectionList() .add( Projections.id()
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.