I am new to hibernate. i have read below line.
Criteria.ALIAS_TO_ENTITY_MAP
Please help me when to use Criteria.ALIAS_TO_ENTITY_MAP
.
Thanks!
The Criteria API allows you to build up a criteria query object programmatically; the org.hibernate.Criteria interface defines the available methods for one of these objects. The Hibernate Session interface contains several overloaded createCriteria () methods.
You can use Hibernate’s @Subselect annotation to map an entity to an SQL query. In the following code snippet, I use this annotation to select the id, the title and the number of reviews of a Book and map them to the BookSummary entity. Before you use this mapping, you need to be aware of two side effects:
In SQL, we will filter the day by means of adding the “WHERE” clause. Here in hibernate, we need to use add () method available for the Criteria object and it is helped to add restrictions for a criteria query. It will have all the comparison operations such as >,<,=, between, etc.
... No. It wouldn't be viable for the guys at hibernate to limit the Criteria specification to obligate the programmer to save the returned instance, and it would kinda go against the OO standards to assume that the criteria object is not altered.
These are generally used with the Result Set Transformers in hibernate. Read the API or see an example. A ResultTransformer is a nice and simple interface that allows you to transform any Criteria result element.
E.g. you can make any Criteria result be returned as a java.util.Map or as a non-entity Bean.
You can read the example for the ALIAS_TO_ENTITY_MAP here. That is,
The kittens collections held by the Cat instances returned by the previous two queries are not pre-filtered by the criteria. If you want to retrieve just the kittens that match the criteria, you must use a ResultTransformer.
List cats = sess.createCriteria(Cat.class)
.createCriteria("kittens", "kt")
.add( Restrictions.eq("name", "F%") )
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
.list();
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
Map map = (Map) iter.next();
Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS);
Cat kitten = (Cat) map.get("kt");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With