Home Hibernate Projections Hibernate Projections (rowCount or countDistinct)



Hibernate Projections (rowCount or countDistinct)
Posted on: March 10, 2008 at 12:00 AM
In this section, you will learn about the hibernate projection with an example.

Hibernate Projections (rowCount or countDistinct)

     

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

Projection Interface: This is an interface that extends the Serializable. An object-oriented representation of a query result set projection in a Criteria query. Built-in projection types are provided by the Projections factory class. The Projection interface might be implemented by application classes that define custom projections.

The following example to count the total number of rows and distinct rows to use the Projections.rowCount() and Projections.countDistinct() method. 

Table Name: Insurance

Here is the code of program:

package roseindia.tutorial.hibernate;

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.Projections;

public class hibernateProjectionExample {

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

  Session sess = null;
  try{
  SessionFactory fact = new 
  Configuration
().configure().buildSessionFactory();
  sess = fact.openSession();
  Criteria crit = sess.createCriteria(Insurance.class);
  crit.setProjection(Projections.rowCount());
  List result = crit.list();
  System.out.println("No. of rows: "+result);
  crit.setProjection(Projections.distinct
   (Projections.countDistinct
("insuranceName")));
  List distList = crit.list();
  System.out.println("Distinct Count:  "+ distList);
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  }
  }

}

Download this Code.

Output:

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

log4j:WARN Please initialize the log4j system properly.

Hibernate: select count(*) as y0_ from insurance this_

No. of rows: [7]

Hibernate: select distinct count(distinct this_.insurance_name) as y0_ from insurance this_

Distinct Count: [5]

Related Tags for Hibernate Projections (rowCount or countDistinct):
chibernatequeryideclassuiinterfaceapplicationobjectiotypescustomclassescountobject-orientedmethodtypevifactorycriteriaserialnumberdistinctintriathisidsetapprowdefinerowsieexamplewithprojectionnattolearnexamextendpresentationssewsearexteilitextendsrepresentationsectionserializableliuseulresultpeimceinasfactormntoutrowcountsticajaceclestdtotalemendmeobjotaproppbuilt-incattorxaxampssuctoratincishaimplementedllmplprefolloweaandaractreprxtwcwingzssrithstabablatiaphatctofacefactendsfinendsjeicaicapleplprojprmindonomolo


More Tutorials from this section

Ask Questions?    Discuss: Hibernate Projections (rowCount or countDistinct)   View All Comments

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.