Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it doesn't depends on the table of the database. Instead of table name, we use class name in HQL. So it is database independent query language.
Advantage of HQL
There are many advantages of HQL. They are as follows:
- database independent
- supports polymorphic queries
- easy to learn for Java Programmer
Query Interface
It is an object oriented representation of Hibernate Query. The object of Query can be obtained by calling the createQuery() method Session interface.
The query interface provides many methods. There is given commonly used methods:
- public int executeUpdate() is used to execute the update or delete query.
- public List list() returns the result of the ralation as a list.
- public Query setFirstResult(int rowno) specifies the row number from where record will be retrieved.
- public Query setMaxResult(int rowno) specifies the no. of records to be retrieved from the relation (table).
- public Query setParameter(int position, Object value) it sets the value to the JDBC style query parameter.
- public Query setParameter(String name, Object value) it sets the value to a named query parameter.
Example of HQL to get all the records
- Query query=session.createQuery("from Emp");//here persistent class name is Emp
- List list=query.list();
Example of HQL to get records with pagination
- Query query=session.createQuery("from Emp");
- query.setFirstResult(5);
- query.setMaxResult(10);
- List list=query.list();//will return the records from 5 to 10th number
Example of HQL update query
- Transaction tx=session.beginTransaction();
- Query q=session.createQuery("update User set name=:n where id=:i");
- q.setParameter("n","Udit Kumar");
- q.setParameter("i",111);
- int status=q.executeUpdate();
- System.out.println(status);
- tx.commit();
Example of HQL delete query
- Query query=session.createQuery("delete from Emp where id=100");
- //specifying class name (Emp) not tablename
- query.executeUpdate();
HQL with Aggregate functions
You may call avg(), min(), max() etc. aggregate functions by HQL. Let's see some common examples:
Example to get total salary of all the employees
- Query q=session.createQuery("select sum(salary) from Emp");
- List<Emp> list=q.list();
- Iterator<Emp> itr=list.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
Example to get maximum salary of employee
- Query q=session.createQuery("select max(salary) from Emp");
Example to get minimum salary of employee
- Query q=session.createQuery("select min(salary) from Emp");
Example to count total number of employee ID
- Query q=session.createQuery("select count(id) from Emp");
It is really a great work and the way in which u r sharing the knowledge is excellent.
ReplyDeleteThanks for helping me to understand HQL concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai
Hi, I read your blog it was really excellent thanks for sharing you knowledge, last weekend i searched blogs relevant this topic then i was found one more blog on HQL if someone wants to know more you can go through this link - http://blogs.innovationm.com/hibernate-query-language/
ReplyDeleteHi, I read your blog it was really excellent thanks for sharing you knowledge, last weekend i searched blogs relevant this topic then i was found one more blog on HQL if someone wants to know more you can go through this link - HQL
ReplyDeleteThanks for sharing this information.InnovationM is the best website design agency and mobile app developmentcompany in London, UK. Top andoid app developers and iOS app developers and Software development company in UK. Best legal website design company in Birmingham, London, Luton, Derby, Sheffield, UK.
ReplyDeleteAcessing the core of any website or software to customize it and develop a quality product, InnovationM is known for its quality assured handcrafted technological solution, going from software development to website design and Mobile application development. InnovationM is the best Mobile app development and Website design Company in London, UK.
ReplyDeleteWe are a Software development and web design company providing custom website solutions, law firm web design services in derby, Luton, Birmingham, Sheffield, Derby, and London, UK, and also popularly known for android app design agency derby, software design agency derby, mobile app design agency derby, app developers luton, software design company derby, app development luton, ios app development company derby, mobile app design derby, iphone app development company derby, bespoke app development derby, software app development derby, android app development company in derby, software development company brighton, app agency oxford, mobile app development company derby, iphone app development derby, ios app development derby, development agency brighton, UK.
And that what we have been known for, InnovationM delivers a quality assure product/ service which is fully dependent on your next virtual need.
For more: www.innovationm.co.uk
oracle sql plsql online training
ReplyDeletego langaunage online training
azure online training
java online training
salesforce online training
hadoop online training
mulesoft online training
linux online training
Very interesting and informative post. Roritech is one of the best web design and development Company. We believe in customer satisfaction i.e provide the work according to customer need within the time.
ReplyDelete