Hibernate Tutorial

Hibernate Create Criteria Projection List with example

Hibernate Create Criteria Projection List:

Hibernatehave an easier way to group projections into single query formation.

public static ProjectionListprojectionList()

Create a newprojection list

projectionList

public void getProjectionListResult() {
    try {
      Session s = getSession();
      Criteria c = s.createCriteria(Employee.class);
 
      c.add(Restrictions.like("name", "C%"));
      c.setProjection(Projections.projectionList()
          .add(Projections.rowCount())
          .add(Projections.groupProperty("name")));
 
      List<Object[]> rows = c.list();
      System.out.println("Projection List In Hibernate:");
      for (Object[] row : rows) {
        System.out.println(row[0] + " and " + row[1]);
      }
    } catch (HibernateException ex) {
      ex.printStackTrace();
    }
  }

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

Hibernate projectionList war

Hibernate projectionList zip