Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

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]

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (post your own) View All Comments Latest 10 Comments:

This site is good tutorial.

Posted by sniperCat on Friday, 04.18.08 @ 13:34pm | #57021

crit.setProjection(Projections.distinct(Projections.countDistinct("insuranceName")));

there is a error in the above line of code when i run the program.

However, if i remove the Projections.distinct function it work fine with correct result.

Posted by Nelson on Saturday, 03.22.08 @ 21:54pm | #53816

here explained too easily about hibernate tools to use orm mapping.. this is useful for beginners to guide.thanks to roseindia..

Posted by RuthraMoorthy.P From Annur on Monday, 03.10.08 @ 18:34pm | #52206

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.