Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Hibernate
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

HQL Group By Clause Example

                         

Group by clause is used to return the aggregate values by grouping on returned component. HQL supports Group By Clause. In our example we will calculate the sum of invested amount in each insurance type. Here is the java code for calculating the invested amount insurance wise:


 

 

 

 
package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 @author Deepak Kumar
 
 * http://www.roseindia.net 
HQL Group by Clause Example

 *  
 */
public class HQLGroupByExample {
  public static void main(String[] args) {
    Session session = null;
    try {
      // This step will read 
hibernate.cfg.xml and prepare hibernate for

      // use
      SessionFactory sessionFactory = 
new Configuration().configure()
          .buildSessionFactory();
      session = sessionFactory.openSession();
      //Group By Clause Example
      String SQL_QUERY = "select sum
(insurance.investementAmount),
insurance.insuranceName "

          "from Insurance insurance 
group by insurance.insuranceName"
;
 Query query = session.createQuery(SQL_QUERY);
 for (Iterator it = 
query.iterate
(); it.hasNext();) {
 Object[] row = (Object[]) it.next();
 System.out.println("
Invested Amount: " 
+ row[0]);
 System.out.println("
Insurance Name: " 
+ row[1]);
      }
      session.close();
    catch (Exception e) {
      System.out.println(e.getMessage());
    finally {
    }
  }
}

To run the example select Run-> Run As -> Java Application from the menu bar. Following out is displayed in the Eclipse console:

Hibernate: select sum(insurance0_.invested_amount) as col_0_0_, insurance0_.insurance_name as col_1_0_ from insurance insurance0_ group by insurance0_.insurance_name

Invested Amount: 3500

Insurance Name: Car Insurance

Invested Amount: 500

Insurance Name: Dental Insurance

Invested Amount: 1550

Insurance Name: Home Insurance

Invested Amount: 1500

Insurance Name: Life Insurance

Invested Amount: 1600

Insurance Name: Medical Insurance

Invested Amount: 1680

Insurance Name: Motorcycle Insurance

Invested Amount: 2600

Insurance Name: Travel Insurance

                         

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

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

can u share the insurance class.....

Posted by harmeet on Friday, 02.1.08 @ 16:12pm | #46855

This article is cool, very interesting, and more informative for the beginners of the Hybernate

Posted by HariBabu on Saturday, 06.9.07 @ 16:13pm | #18718

union select sysdate from dual

Posted by rj on Thursday, 02.1.07 @ 11:30am | #5149

Latest Searches:
multi-dimensional arra
Track wireless session
mapping.findForward
Photoshop Tutorial
table grid
insert BFILE data type
Javascript Links and B
abstract.
java Swing button smok
Photoshop Animation Bl
logout page in jsp
what are the exception
weblogic 8.1
get screen width and h
download file using js
list out the advantage
HOW TO UPLOAD IMAGE FR
how to use email valid
velocity
change frame font colo
file read and write i
Handling Mouse Clicks
J2ME
registration connect t
pdf fromatting
creeate arry
decimal to char
Photoshop Textures and
nested logic
Getparameter
notepad code
prepareStatement
Flash Math Physics Cal
GEL
Creating Midlet Applic
Black
insert data to text fi
rules to use java coll
Java Mysql
java cancel button
ASP.NET .NET Security
ejb example, jboss
hiding message box in
shashi
get hostname
ASP Database Related D
write in .properties
String getChars(int sr
how to execute the str
timeline using javascr
how to retrieve image
How to Validate a File
connect jsp to sql
scjp 1.5
clear values from a fo
10 Minutes Guide to An
how to send sms to mob
remove rectangle awt
Retrieving Data From d
breakதà¯?à®?à¯??தà
convert text file to i
multipartDateFormat
droping column in tabl
ั?ะ??ั?ะ??????
break��?��?�
html samples
Copy One Database Tabl
mysql primary key,fori
midlet to create anim
jQuery to Retrieve Ser
date format
Merge Sort In Java
Ñ?â?£?Ñ?â????Ñ?â?£?Ñ?â
EJB
Photoshop Text Effects
Checked exceptions
tree moss
Ant
ASP.NET .NET Security
convert binary to acsi
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.