Hibernate - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate HQL

Hibernate HQL

shape Description

Hibernate HQL is the own query language of Hibernate, the main advantage of HQL is database independent, HQL works on persistency class and their properties, but where as SQL works on tables, columns.To perform Curd operations (insert, update, delete) on bulk objects at a time, hibernate has three Query languages.

shape More Info

HQL is a Query language, HQL Queries are similar to SQL Queries, but HQL Queries are Database independent and SQL Queries are Database dependent.
  • In HQL Queries,in place of table name, the POJO class name is kept and in place of column names, variable names of a POJO class are used.
  • HQL Query is used in the POJO class names and variable names. So, HQL Query is called as an object-oriented form of SQL.
  • Internally Hibernate was translated from HQL Query to SQL Query with respect to a database, using dialect classes.
  • HQL can be used for executing both select and Non-select operations on the database.
  • HQL select operations can be constructed.
    [sql] SQL ----select * from SPLessons; HQL---from SPLessons lessons; [/sql]
    • In Hibernate, reading thecomplete rows in a database is called full entity and reading only particular rows of a column is called partial entity.
    • In Hibernate, to read one or more full entity's from database then one need to begin HQL, select Query directly .Start with from keyword, and read the partial entity then HQL Query start with the select keyword.
    [sql] SQL------ select * from SPLessons where salary=8000; HQL----- from SPLessons lessons where lessons.salary=8000; SQL ----- select name, salary from SPLessons; HQL------select lessons.name, lessons.sal from SPLessons lessons; [/sql]
    • If want to set dynamic values to HQL command then use either index parameter or named parameter.In Hibernate, the index of index parameter starts with Zero (0) and named parameter start with " : " symbol.In JDBC, the index parameter starts with One (1).
    • If want to execute HQL command then create a Query object.
    • To get Query object for HQL command, createQuery() of Session interface can be called.
    [sql] Query qry=session.createQuery(" HQL Querys");[/sql]
    • If HQL command performed select operation on a database then it calls list().
    • If it is a Non-select command, then call executeUpdate().
    [sql] Query query1=session.createQuery(" HQL select command"); List list=query1.list(); Query query2=session.createQuery(" HQL Non select command "); int i=query2.executeUpdate(); [/sql]
    • When HQL select command is executed to read complete entity then a list object contains a collection of object type POJO class. When select command is executed, Internally Hibernate performs this operation.
    [sql]Query qry=session.createQuery("from IToolsInfo itoolsinfo"); List list=qry.list();[/sql] Then list contain POJO Class object values. [sql]Iteratot iterator=list.iterator(); while(iterator.hasNext()) { IQuickInfo iquickinfo=iterator.next(); System.out.println(iquickinfo); } [/sql]
    • If HQL select command is to read partial entity, with more than one entity then a list object contains the collection of objects of type Object[].
      [sql]Query qry=session.createQuery("select itoolsinfo.ename, itoolsinfo.sal from IToolsInfo itoolsinfo"); List list=qry.list();[/sql] [java]Iteratot iterator=list.iterator(); while(iterator.hasNext()) { Object object[]=iterator.next(); System.out.println(object[]); }[/java]
    • If HQL select command reads one property then collection object contains a collection of objects of that property data type.
    [java]Query qry=session.createQuery("select itoolsinfo.ename from IToolsInfo itoolsinfo"); List list=qry.list();[/java] [java]Iteratot iterator=list.iterator(); while(iterator.hasNext()) { String string=iterator.next(); System.out.println(string); }[/java]
    • In HQL, a Non-select command is constructed, then transaction object is maindatry.
    [sql]Query qry=session.createQuery("update IToolsInfo itoolsinfo set itoolsinfo.sal=10000 where technology="java"); Transaction transaction=session.beginTransaction(); int i=qry.executeUpdate(); transaction.commit(); [/sql]

    Summary

    shape Points

    • HQL is object oriented and database independent.
    • To perform bulk operations on hibernate application HQL will be use full.
    • Single and multiple rows will be performed by HQL at a time.