Business web hosting - Querying with criteria and example Result transformers are

Querying with criteria and example Result transformers are also useful if you want to retrieve aliased entities in a join query: Criteria crit = session.createCriteria(Item.class) .createAlias(”bids”, “b”) .createAlias(”seller”, “s”) .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); List result = crit.list(); for (Object aResult : result) { Map map = (Map) aResult; Item item = (Item) map.get(Criteria.ROOT_ALIAS); Bid bid = (Bid) map.get(”b”); User seller = (User) map.get(”s”); … } First, a criteria query is created that joins Item with its bids and seller associations. This is an SQL inner join across three tables. The result of this query, in SQL, is a table where each result row contains item, bid, and user data almost the same as shown in figure 14.2. With the default transformer, Hibernate returns only Item instances. And, with the DISTINCT_ROOT_ENTITY transformer, it filters out the duplicate Item references. Neither option seems sensible what you really want is to return all information in a map. The ALIAS_TO_ENTITY_MAP transformer can marshal the SQL result into a collection of Map instances. Each Map has three entries: an Item, a Bid, and a User. All result data is preserved and can be accessed in the application. (The Criteria.ROOT_ALIAS is a shortcut for “this”.) Good use cases for this last transformer are rare. Note that you can also implement your own org.hibernate.transform.ResultTransformer. Furthermore, HQL and native SQL queries also support a ResultTransformer: Query q = session.createQuery( “select i.id as itemId,” + ” i.description as desc,” + ” i.initialPrice as price from Item i”); q.setResultTransformer( Transformers.aliasToBean(ItemDTO.class) ); This query now returns a collection of ItemDTO instances, and the attributes of this bean are populated through the setter methods setItemId(), setDesc(), and setPrice(). A much more common way to define what data is to be returned from a query is projection. The Hibernate criteria supports the equivalent of a SELECT clause for simple projection, aggregation, and grouping.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Leave a Reply