|
Answer» Hibernate uses a language called HQL (Hibernate Query Language) which is an Object-oriented query language and works with persistent objects and their properties. Queries written in HQL are translated by Hibernate into SQL which is the language understood by the database which is being queried. Even though Hibernate supports SQL statements written in native SQL, it is advisable to use HQL as it helps take advantage of Hibernate's SQL generation and caching strategies. Some methods of Query interface: - To create a new instance of Query for a HQL string we use the session.createQuery(String QUERYSTRING) METHOD.
String queryString = "select * from DEPARTMENT"
Query query = session.createQuery(queryString);
List list = query.list(); - To filter collections (which is the RESULT of a query) BASED on a filter string, the session.createFilter(Object collection, String filterString) is used.
Collection<Child> CHILDS = parent.getChilds();
Query query = session.createFilter(childs," where this.name like 'S%'");
List list = query.list(); - To create a criteria, the method session.createCriteria(Class persistentClass) is used
Criteria criteria=session.createCriteria(Parent.class);
criteria.add(Restrictions.like("name","S%");
List list=criteria.list();
The method session.createSQLQuery(String queryString) is used to create SQL queries using Native SQL.
String sqlString="SELECT * FROM PARENT where name like 'S%'";
SQLQuery sqlQuery=session.createSQLQuery(sqlString);
List list= sqlQuery.list();
|