Home Hibernate Examples Criteria Hibernate Criteria Projections.distinct

Ask Questions?

View Latest Questions


 
 

Hibernate Criteria Projections.distinct
Posted on: April 12, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn about Hibernate Criteria Projections.distinct

Hibernate Criteria Projections.distinct

The Hibernate Criteria Projections.distinct selects the distinct records from the table

ProjectionList projectionList = Projections.projectionList();
	projectionList.add(Projections.property("rollNo"));
	projectionList.add(Projections.property("name"));
	projectionList.add(Projections.property("course"));
	criteria.setProjection(projectionList);
	criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
	List list = criteria.list();

Please Consider the example given below

ProjectionDistinct.java

package net.roseindia.main;

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

import net.roseindia.bean.Student;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

public class ProjectionDistinct {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Student.class);
		ProjectionList projectionList = Projections.projectionList();
		projectionList.add(Projections.property("rollNo"));
		projectionList.add(Projections.property("name"));
		projectionList.add(Projections.property("course"));
		criteria.setProjection(projectionList);
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		List list = criteria.list();
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		
	}
}

When you run this application it will display message as shown below:

Hibernate: select this_.roll_no as y0_, this_.name as y1_, this_.course as y2_ from student this_
MCA
B.Tec
BCA
MCA
MBA
DCA
MCA

Download Complete Source Code

Related Tags for Hibernate Criteria Projections.distinct :


DMCA.com