My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

Querying in Hibernate

Best approach to follow for HQL execution in Hibernate

1) session.createSQLQuery:
Coming to the session.createSQLQuery is not suggested to follow, because that will execute SQL only using that we can't able to execute HQL query. Because, we are trying to follow object oriented mapping with database so we should try to avoid createSQLQuery.
2) session.createQuery:
It is same like executing SQL query, only difference here like will use always objects instead of tables because it will accept HQL query. HQL queries are parsed with an ANTLR-based parser and then the resulting AST is turned into SQL. HQL we can use for static queries. To write HQL, we should have knowledge to form a query then we can able to write HQL. For complex and dynamic queries are not useful.
3) session.createCriteria:
It is like API, this is not at all looking like HQL/SQL formation. We can write criteria like forming object then adding restrictions (conditions)/projections (particular columns). For complex and dynamic queries this will be very useful. Especially for search queries criteria is the best approach. To generate criteria,No need to parse before generate SQL.To write criteria no need of knowledge on forming queries and also it is following object oriented mapping. To control eagerly fetching on top of HBM, we can set FETCH mode as EAGER. Using this whatever objects we want to get based on eagerly fetching we can get only those objects not all whatever we have mentioned relationship in hibernate mapping file(hbm). Criteria API provide one distinct feature that Neither SQL or HQL provides. ie. it allows compile time checking of a query.
http://stackoverflow.com/questions/4401240/criteria-v-s-hql-who-is-faster
4) session.createFilter:
This is not regarding HQL or SQL. This collection filter is a special type of query that can be applied to a persistent collection or array. The query string can refer to this, meaning the current collection element. Whenever we want to apply condition on collection we can use createFilter method instead of doing manually.
Collection blackKittens = session.createFilter(
       pk.getKittens(),
       "where this.color = ?")
       .setParameter( Color.BLACK, Hibernate.custom(ColorUserType.class) )
       .list()
);

The returned collection is considered a bag that is a copy of the given collection. The original collection is not modified. This is contrary to the implication of the name "filter", but consistent with expected behavior.
Observe that filters do not require from clause, although they can have one if required. Filters are not limited to returning the collection elements themselves.
Collection blackKittenMates = session.createFilter(
       pk.getKittens(),
       "select this.mate where this.color = eg.Color.BLACK.intValue")
       .list();
Even an empty filter query is useful, e.g. to load a subset of elements in a large collection:
Collection tenKittens = session.createFilter(
       mother.getKittens(), "")
       .setFirstResult(0).setMaxResults(10)
       .list();
Especially these filters will be useful whenever we want to apply condition on collections we can apply and based on that we can perform actions like I have list of solutions based on conditions I need to make it active or inactive. In that case which is not satisfying condition, I will get the values using createFilter will make "INACTIVE". As well as "ACTIVE" which objects are satisfying conditions.