Hibernate Create Criteria Projections example xml and annotation

Projections in hibernate helps to retrieve data based in SQL aggregate functions.

avg(String propertyName)

     A property average value


count(String propertyName)

     A property value count


countDistinct(String propertyName)

     A distinct property value count


distinct(Projection proj)

     Create a distinct projection from a projection


groupProperty(String propertyName)

     A grouping property value


max(String propertyName)

     A property maximum value


min(String propertyName)

     A property minimum value



    The query row count, ie.


sum(String propertyName)

    A property value sum

Projections example

	public void getProjectionResult() {
		try {
			Session s = getSession();

			Criteria c = s.createCriteria(Employee.class);

			List avg = c.list();
			System.out.println("Average Salary :" + avg);

			List min = c.list();
			System.out.println("Minimum Age :" + min);

			List max = c.list();
			System.out.println("Maximum Experience :" + max);

			List count = c.list();
			System.out.println("Employee Count :" + count);

			List sum = c.list();
			System.out.println("Sum Of Salary :" + sum);

			List distinct = c.list();
			System.out.println("Distinct values in Name:" + distinct);

		} catch (Exception e) {


package com.candidjava.hibernate;

import java.io.Serializable;

public class Employee implements Serializable {
	private long Id;
	private String name;
	private int age;
	private String gender;
	private int salary;
	private int experience;
	private String address;
	private long mobile;

	public long getId() {
		return Id;

	public void setId(long id) {
		Id = id;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;

	public String getGender() {
		return gender;

	public void setGender(String gender) {
		this.gender = gender;

	public int getSalary() {
		return salary;

	public void setSalary(int salary) {
		this.salary = salary;

	public int getExperience() {
		return experience;

	public void setExperience(int experience) {
		this.experience = experience;

	public String getAddress() {
		return address;

	public void setAddress(String address) {
		this.address = address;

	public long getMobile() {
		return mobile;

	public void setMobile(long mobile) {
		this.mobile = mobile;




 Create Criteria projection war

 Create Criteria projection zip

Related Post