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

 

rowCount()

    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);

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

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

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

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

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

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

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} 


POJO

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;
	}

}

 

Download

 Create Criteria projection war

 Create Criteria projection zip



Related Post

Comments


©candidjava.com