0% found this document useful (0 votes)
150 views3 pages

HQL (Hibernate Query Language) Tutorial

HQL is a query language for Hibernate that is similar to SQL but independent of the underlying database. It allows querying based on class names rather than table names. This document provides examples of performing queries, updates, deletes, and using aggregate functions with HQL.

Uploaded by

Arun Vijay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views3 pages

HQL (Hibernate Query Language) Tutorial

HQL is a query language for Hibernate that is similar to SQL but independent of the underlying database. It allows querying based on class names rather than table names. This document provides examples of performing queries, updates, deletes, and using aggregate functions with HQL.

Uploaded by

Arun Vijay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

HQL(HibernateQueryLanguage)TutorialwithExamples

1.
HibernateQueryLanguage(HQL)issameasSQL(StructuredQueryLanguage)butitdoesn'tdepends
onthetableofthedatabase.Insteadoftablename,weuseclassnameinHQL.Soitisdatabase
independentquerylanguage.

AdvantageofHQL
TherearemanyadvantagesofHQL.Theyareasfollows:
databaseindependent
supportspolymorphicqueries
easytolearnforJavaProgrammer

QueryInterface
ItisanobjectorientedrepresentationofHibernateQuery.TheobjectofQuerycanbeobtainedby
callingthecreateQuery()methodSessioninterface.
Thequeryinterfaceprovidesmanymethods.Thereisgivencommonlyusedmethods:
1.publicintexecuteUpdate()isusedtoexecutetheupdateordeletequery.
2.publicListlist()returnstheresultoftheralationasalist.
3.publicQuerysetFirstResult(introwno)specifiestherownumberfromwhererecordwillbe
retrieved.
4.publicQuerysetMaxResult(introwno)specifiestheno.ofrecordstoberetrievedfromtherelation
(table).
5.publicQuerysetParameter(intposition,Objectvalue)itsetsthevaluetotheJDBCstylequery
parameter.
6.publicQuerysetParameter(Stringname,Objectvalue)itsetsthevaluetoanamedquery
parameter.

ExampleofHQLtogetalltherecords
1.Queryquery=session.createQuery("fromEmp")//herepersistentclassnameisEmp
2.Listlist=query.list()

ExampleofHQLtogetrecordswithpagination

1.Queryquery=session.createQuery("fromEmp")
2.query.setFirstResult(5)
3.query.setMaxResult(10)
4.Listlist=query.list()//willreturntherecordsfrom5to10thnumber

ExampleofHQLupdatequery
1.Transactiontx=session.beginTransaction()
2.Queryq=session.createQuery("updateUsersetname=:nwhereid=:i")
3.q.setParameter("n","UditKumar")
4.q.setParameter("i",111)
5.intstatus=q.executeUpdate()
6.System.out.println(status)
7.tx.commit()

ExampleofHQLdeletequery
1.Queryquery=session.createQuery("deletefromEmpwhereid=100")
2.//specifyingclassname(Emp)nottablename
3.query.executeUpdate()

HQLwithAggregatefunctions
Youmaycallavg(),min(),max()etc.aggregatefunctionsbyHQL.Let'sseesomecommonexamples:

Exampletogettotalsalaryofalltheemployees
1.Queryq=session.createQuery("selectsum(salary)fromEmp")
2.List<Emp>list=q.list()
3.Iterator<Emp>itr=list.iterator()
4.while(itr.hasNext()){
5.System.out.println(itr.next())
6.}

Exampletogetmaximumsalaryofemployee
1.Queryq=session.createQuery("selectmax(salary)fromEmp")

Exampletogetminimumsalaryofemployee
1.Queryq=session.createQuery("selectmin(salary)fromEmp")

ExampletocounttotalnumberofemployeeID

1.Queryq=session.createQuery("selectcount(id)fromEmp")

Exampletogetaveragesalaryofeachemployees
1.Queryq=session.createQuery("selectavg(salary)fromEmp")
NextTopicHCQL

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy