Hibernate Projections

In this section, you will learn about the hibernate projections with an appropriate example.

Hibernate Projections


In this section, you will learn about the hibernate projections with an appropriate example.

Projections: The package Criteria is used as a framework by the applications just to build the new kinds of projection. may be used by applications as a framework for building new kinds of Projection. In general Projection means to retrieve while in case of SQL Projection means "Select" clause. Most of the applications uses the built-in projection types by means of the static factory methods of this class.


package net.roseindia;

public class Product {
  private int id;
  private String name;
  private double price;
  private Dealer dealer;
  private int did;
  public Product(String name, double price) {
  // TODO Auto-generated constructor stub
  this.name = name;
  this.price = price;
  public Product() {
  // TODO Auto-generated constructor stub
  public Dealer getDealer() {
  return dealer;
  public void setDealer(Dealer dealer) {
  this.dealer = dealer;
  public double getDid() {
  return did;
  public void setDid(int did) {
  this.did = did;
  public int getId() {
  return id;
  public void setId(int id) {
  this.id = id;
  public String getName() {
  return name;
  public void setName(String name) {
  this.name = name;
  public double getPrice() {
  return price;
  public void setPrice(double price) {
  this.price = price;


ProjectionList:  is the list of projection instances which are result of Query's object. 
Criteria API:  enables us to specify criteria based on various.

In the class projectionExample.java,  first we create the session object with the help of the SessionFactory interface. Then we use the createQuery() method of the Session object which returns a Query object. Now we use the openSession() method of the SessionFactory interface simply to instantiate the Session object.
Then we obtain the criteria object simply by invoking the createCriteria() method of the Session's object. Now we create a projectionList object add the fields having properties "name" and "price". Set it to the Criteria object by invoking the setProjection() method and passing the projectList object into this method and then add this object into the List interface's list object and iterate this object list object to display the data contained in this object.


package net.roseindia;

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

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

public class projectionExample {

 @param args
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Session sess = null;
  SessionFactory sfact = new 
  sess = sfact.openSession();
  Criteria crit = sess.createCriteria(Product.class);
  ProjectionList proList = Projections.projectionList();
  List list = crit.list();
  Iterator it = list.iterator();
  System.out.println("No any data!");
  Object[] row = (Object[])it.next();
  for(int i = 0; i < row.length;i++){
  catch(Exception e){




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

log4j:WARN Please initialize the log4j system properly.

Hibernate: select this_.name as y0_, this_.price as y1_ from Product this_

Product Name  Price

Computer   23000.0

Mobile     15000.0

Laptop      200.0

Keyboard    1500.0

PenDrive  200.0

HardDisk  2500.0

Computer   100.0