Hibernate Aggregrate Function MIN
In this section, you will learn about Hibernate MIN aggregrate function or aggregrate function MIN. The MIN(property_name) function takes a parameter as property_name.
The MIN function gives you the minimum value of given property in your selected result. The hibernate aggregrate function MIN calculate the minimum value of selected column according to conditions
You will see the running example of hibernate aggregrate MIN():
package developerhelpway.hibernate.hql.aggregateFunction;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class MinAggregateFunction {
/**
* @param args
*/
public static void main(String[] args) {
Session sess = null;
try{
SessionFactory sf = new Configuration().
configure().buildSessionFactory();
sess = sf.openSession();
Transaction tr = sess.beginTransaction();
String hql = "SELECT MIN(e.empSal) FROM Employee e";
Query query = sess.createQuery(hql);
List maxResultList = query.list();
System.out.println("MIN Employee sal: "+
maxResultList != null ? maxResultList.get(0) : 0.0);
tr.commit();
}catch(Exception ex){
ex.printStackTrace();
}finally{
sess.close();
}
}
}
|
Download Code
Employe table:
OutPut:
