|
Projection List
The following example shows how to use Hibernate projection list API.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.criteria;
import java.util.Arrays;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class CriteriaProjectionListTest {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Company.class);
criteria.setProjection(Projections.projectionList()
.add( Projections.rowCount() )
.add( Projections.avg("employeeCount") )
.add( Projections.max("projectCount") )
);
Object [] stats = (Object []) criteria.uniqueResult();
System.out.println(Arrays.toString(stats));
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
[10, 40294.4, 8478]
|
|