Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use Criteria.ALIAS_TO_ENTITY_MAP in hibernate?

Tags:

hibernate

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!

like image 215
user1016403 Avatar asked Jan 18 '12 11:01

user1016403


People also ask

What is criteria query in hibernate?

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.

How do I map an entity to a query in hibernate?

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:

How to filter the day by criteria in hibernate?

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.

Is it possible to limit the amount of instances hibernated by criteria?

... 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.


1 Answers

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");
}
like image 137
ManuPK Avatar answered Oct 03 '22 01:10

ManuPK