Share on Google+Share on Google+

JPA Avg Function

In this section, you will learn the avg of aggregate function. This method calculate an average of given data.

JPA Avg Function


In this section, you will learn the avg of aggregate function. This method calculate an average of  given data.

JPA Avg() function:

Query query=em.createQuery
("SELECT p FROM Product p WHERE p.price >
(SELECT AVG(p.price) FROM p)"

This query select all records from the product table which have the product price is grater than the average of product price.

For this application, you need the following artifacts:

  1. Database Table: product
  2. Model Class:
  3. Main Class:

Database Table: product

CREATE TABLE `product` ( 
`id` int(11) NOT NULL auto_increment, 
`itemName` varchar(50) NOT NULL, 
`price` double NOT NULL, 

Model Class:


package roseindia;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

* @author Administrator
public class Product {

private int id;
* @return the id
public int getId() {
return id;

* @param id the id to set
public void setId(int id) { = id;

private String itemName;
* @return the itemName
public String getItemName() {
return itemName;

* @param itemName the itemName to set
public void setItemName(String itemName) {
this.itemName = itemName;

private double price;
* @return the price
public double getPrice() {
return price;

* @param price the price to set
public void setPrice(double price) {
this.price = price;


Main Class:

package roseindia;

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

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

* @author Administrator
public class JPAAvgFunction {

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

EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
Query query=em.createQuery("SELECT p FROM Product p WHERE p.price > (SELECT AVG(p.price) FROM p)");
List pList=query.getResultList();
Iterator pIterator=pList.iterator();
Product pro=(Product);
System.out.print("Item Name:"+pro.getItemName());
System.out.print(" Price:"+pro.getPrice());


log4j:WARN No appenders could be found for logger

log4j:WARN Please initialize the log4j system properly.

Hibernate: select as id1_, product0_.itemName
as itemName1_, product0_.price as price1_ from product
product0_ where product0_.price>(select AVG(product0_.price)
 from product product0_)

Item Name:Mobile Price:8000.0

Item Name:T.V. Price:6000.0



Posted on: March 17, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JPA Avg Function  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image