Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Hibernate Query Language 
 

Hibernate Query Language or HQL for short is extremely powerful query language. HQL is much like SQL and are case-insensitive, except for the names of the Java Classes and properties.

 

Hibernate Query Language

                         

Hibernate Query Language or HQL for short is extremely powerful query language. HQL is much like SQL  and are case-insensitive, except for the names of the Java Classes and properties. Hibernate Query Language is used to execute queries against database. Hibernate automatically generates the sql query and execute it against underlying database if HQL is used in the application. HQL is based on the relational object models and makes the SQL object oriented. Hibernate Query Language uses Classes and properties instead of tables and columns. Hibernate Query Language is extremely powerful and it supports Polymorphism, Associations, Much less verbose than SQL.

There are other options that can be used while using Hibernate. These are Query By Criteria (QBC) and Query BY Example (QBE) using Criteria API and the Native SQL queries. In this lesson we will understand HQL in detail.

Why to use HQL?

  • Full support for relational operations: HQL allows representing SQL queries in the form of objects. Hibernate Query Language uses Classes and properties instead of tables and columns.
       
  • Return result as Object: The HQL queries return the query result(s) in the form of object(s), which is easy to use. This elemenates the need of creating the object and populate the data from result set.
       
  • Polymorphic Queries: HQL fully supports polymorphic queries. Polymorphic queries results the query results along with all the child objects if any.
       
  • Easy to Learn: Hibernate Queries are easy to learn and it can be easily implemented in the applications.
       
  • Support for Advance features: HQL contains many advance features such as pagination, fetch join with dynamic profiling, Inner/outer/full joins, Cartesian products. It also supports Projection, Aggregation (max, avg) and grouping, Ordering, Sub queries and SQL function calls.
      
  • Database independent: Queries written in HQL are database independent (If database supports the underlying feature).

 

Understanding HQL Syntax
Any Hibernate Query Language may consist of following elements:

  • Clauses
  • Aggregate functions
  • Subqueries

Clauses in the HQL are:

Aggregate functions are:

Subqueries
Subqueries are nothing but its a query within another query. Hibernate supports Subqueries if the underlying database supports it.

 

                         

» View all related tutorials
Related Tags: sql c hibernate api com class ant comparison import io property method sed const operators expression operator port criteria name

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

good comments

Posted by arabinda on Tuesday, 08.7.07 @ 21:51pm | #22883

hi,

y it is not working

Posted by sabi on Saturday, 12.9.06 @ 18:18pm | #708




hi,

i want to delete some records from 1 table and then i have to insert new one.I write the HQL for this like the method below.in this i wrote delete first and then insert(look at last section DELETE and SAVE operations only).but when committed it tries to perform insert first so constraint violation happens and DB2 state err coming.can u give an example for delete and insert inside 1 transaction to 1 table.I want it soon .I stuck with that problem in my project..


shinoy


Posted by shinoy on Saturday, 12.9.06 @ 18:17pm | #707

hi,

i want to delete some records from 1 table and then i have to insert new one.I write the HQL for this like the method below.in this i wrote delete first and then insert(look at last section DELETE and SAVE operations only).but when committed it tries to perform insert first so constraint violation happens and DB2 state err coming.can u give an example for delete and insert inside 1 transaction to 1 table.I want it soon .I stuck with that problem in my project..





private void updateContact(CustomerContactForm customerContactForm,GDMSUserDTO roleUser)
{


Timestamp CurrentTS = new Timestamp(System.currentTimeMillis());
SessionFactory sf = null;
Session hibSession = null;
StringBuffer cusQuery=new StringBuffer();
StringBuffer cusQueryCmnt=new StringBuffer();
StringBuffer cusQueryChgLog=new StringBuffer();
StringBuffer cusQueryContDoc=new StringBuffer();

hibSession = HibernateUtil.getSessionFactory().openSession();
Transaction tx = hibSession.beginTransaction();
Long custContSkey=customerContactForm.getCustContSkey();//new Long(45);
try {
List custCont = new ArrayList();
cusQuery.append("from fit.gdms.schemadao.CustomerContact con ");
cusQuery.append("where con.custContSkey ="+custContSkey); //:custContSkey
custCont = hibSession.createQuery(new String(cusQuery)).list();

if (custCont.size()> 0)
{
CustomerContact custContact = (CustomerContact)custCont.get(0);
if(!(custContact.getCustContName()==customerContactForm.getContactName()))
{

custContact.setCustContName(customerContactForm.getContactName());
Customer customer=new Customer();
customer.setCustSkey(customerContactForm.getCustSkey());
custContact.setCustomer(customer);
Language language=new Language();
language.setLanguageCd(customerContactForm.getLanguageCd());
custContact.setLanguage(language);
ContactPosition contPosition=new ContactPosition();
contPosition.setContPosCd(customerContactForm.getPosition());//(contPosCd);
custContact.setCustContName(customerContactForm.getContactName());
custContact.setContactPosition(contPosition);
custContact.setContDept(customerContactForm.getDepartment());
custContact.setContEmail(customerContactForm.getEmail());
custContact.setContPhone1(customerContactForm.getPhone1());
custContact.setContPhone2(customerContactForm.getPhone2());
custContact.setContFax1(customerContactForm.getFax1());
custContact.setContFax2(customerContactForm.getFax2());
custContact.setCustConAddr1(customerContactForm.getLine1());
custContact.setCustConAddr2(customerContactForm.getLine2());
custContact.setCustConAddr3(customerContactForm.getLine3());
custContact.setCustConAddr4(customerContactForm.getPlaceCity());
custContact.setCustConAddr5(customerContactForm.getState());
custContact.setCustConAddr6(customerContactForm.getCountry());
custContact.setCustConAddr7(customerContactForm.getZipPostalCode());
if(("OTH").equalsIgnoreCase(customerContactForm.getPosition()))
{
custContact.setContPosDesc(customerContactForm.getPosDocdesc());

}

custContact.setLastUpdId(roleUser.getUserCNUM());
custContact.setLastUpdTs(CurrentTS);
hibSession.update(custContact);
}

List custContCmntList = new ArrayList();
cusQueryCmnt.append("from fit.gdms.schemadao.CustContCmnt cmnt ");
cusQueryCmnt.append("where cmnt.customerContact.custContSkey ="+custContSkey);//:custContSkey");
custContCmntList = hibSession.createQuery(new String(cusQueryCmnt)).list();
if (custContCmntList.size()> 0)
{

CustContCmnt custContCmnt = (CustContCmnt)custContCmntList.get(0);
custContCmnt.setCmntLogText(customerContactForm.getComments());
custContCmnt.setCustomerContact(custContact);//.setCustomerContact(custContact);
custContCmnt.setLastUpdId(roleUser.getUserCNUM());
custContCmnt.setLastUpdTs(CurrentTS);
hibSession.update(custContCmnt);

}

List custCntChgLogList = new ArrayList();
cusQueryChgLog.append("from fit.gdms.schemadao.CustCntChgLog cccl ");
cusQueryChgLog.append("where cccl.customerContact.custContSkey ="+custContSkey);//:custContSkey");
custCntChgLogList = hibSession.createQuery(new String(cusQueryChgLog)).list();
if (custCntChgLogList.size()> 0)
{

CustCntChgLog custCntChgLog = (CustCntChgLog)custCntChgLogList.get(0);
custCntChgLog.setCustomerContact(custContact);
custCntChgLog.setChgItem("CustomerContactName");
custCntChgLog.setLastUpdId(roleUser.getUserCNUM());
custCntChgLog.setLastUpdTs(CurrentTS);
custCntChgLog.setOldChgValue(custCntChgLog.getNewChgValue());
custCntChgLog.setNewChgValue(customerContactForm.getContactName());
hibSession.update(custCntChgLog);
}
List custContDocList = new ArrayList();
cusQueryContDoc.append("from fit.gdms.schemadao.CustContDoc ccd ");
cusQueryContDoc.append("where ccd.customerContact.custContSkey ="+custContSkey);//:custContSkey");
custContDocList = hibSession.createQuery(new String(cusQueryContDoc)).list();
if (custContDocList.size()> 0)
{

for(int i=0;i<custContDocList.size();i++)
{
CustContDoc custContDoc = (CustContDoc)custContDocList.get(i);
hibSession.delete(custContDoc);

}
}
if(customerContactForm.getContDoc()!=null)
{

for (int i = 0; i< customerContactForm.getContDoc().length ;i++)
{
String[] contDoc = customerContactForm.getContDoc();

String contDocCd = contDoc[i];
CustContDoc custContDoc = new CustContDoc();
ContactDocmt contDocmt = new ContactDocmt();
contDocmt.setContDocCd(contDocCd);
Language lang=new Language();
lang.setLanguageCd(customerContactForm.getLanguageCd());
if (("OTH").equalsIgnoreCase(contDocCd) )
{

custContDoc.setContDocDesc(customerContactForm.getContDocDesc());

}
custContDoc.setLanguage(lang);
custContDoc.setContactDocmt(contDocmt);
custContDoc.setCustomerContact(custContact);
custContDoc.setLastUpdId(roleUser.getUserCNUM());
custContDoc.setLastUpdTs(CurrentTS);
hibSession.save(custContDoc);


}


}

tx.commit();
//hibSession.flush();
hibSession.close();
}
}catch (HibernateException e) {

tx.rollback();
System.out.println("err "+e);
e.getStackTrace();

}catch (Exception e) {
e.printStackTrace();


} finally {


//hibSession.close();
}
return ;

}

Posted by shinoy v on Saturday, 12.9.06 @ 18:14pm | #705

A very good tutorial which is very simple to understand. thanks to roseindia.net..

Posted by SANTANU on Monday, 12.4.06 @ 20:18pm | #393

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.