Hibernate Avg() Function (Aggregate Functions)

In this section, we will show you, how to use the avg() function. Hibernate supports multiple aggregate functions.


Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment

Hibernate Avg() Function (Aggregate Functions)


In this section, we will show you, how to use the avg() function. Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg(...), sum(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all...) ) calculated from property values of all objects satisfying other query criteria. 

Following is a aggregate function (avg() function) with their respective syntax.

avg( [ distinct | all ] object.property):

The avg() function aggregates the average value of the given column. 

Table Name: insurance

ID insurance_name invested_amount investement_date
2 Life Insurance 25000 0000-00-00 00:00:00
1 Givan Dhara 20000  2007-07-30 17:29:05
3 Life Insurance   500 2005-10-15 00:00:00
4 Car Insurance    2500 2005-01-01 00:00:00
5 Dental Insurance 500 2004-01-01 00:00:00
Life Insurance 900 2003-01-01 00:00:00
Travel Insurance   2000  2005-02-02 00:00:00

Here is the java code to retrieve the average value of "invested_amount" column from insurance table:

package roseindia.tutorial.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateHQLAvgFunction {

 * @Vinod kumar
  public static void main(String[] args) {
  // TODO Auto-generated method stub

  Session sess = null;
  try {
  SessionFactory fact = new Configuration()
  sess = fact.openSession();
  String SQL_QUERY = "select avg
(investementAmount) from Insurance insurance"
  Query query = sess.createQuery(SQL_QUERY);
  List list = query.list();
  System.out.println("Average of 
Invested Amount: " 
+ list.get(0));
  catch(Exception e){


Download this Code.


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select avg(insurance0_.invested_amount) as col_0_0_ from insurance insurance0_

Average of Invested Amount: 7342.8571


Share on Google+Share on Google+

Hibernate Avg() Function (Aggregate Functions)

Posted on: February 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Avg() Function (Aggregate Functions)   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 7, 2012
result in null value

this program whwn executed returns null...plz give some solution...
July 24, 2013
getting null as o/p

Hi i'm getting null as output instead avg. tried this way also.. Iterator it = list.iterator();//way 2 System.out.println("avgPrice"); while(it.hasNext()) { System.out.println(it.next()); } but same result
July 24, 2013
solution: for null result

In the query. give as eg: select avg(ins.investementAmount) from Insurance ins reference.property
Pham Tu
August 11, 2013
This program throw null pointer ex

This program throw null pointer ex in this line of code List list = query.list(); So, please tell me why?