HQL Group By Clause Example

Group by clause is used to return the aggregate values by grouping on returned component. HQL supports Group By Clause. In our example we will calculate the sum of invested amount in each insurance type. Here is the java code for calculating the invested amount insurance wise:


 

package roseindia.tutorial.hibernate;
import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 @author Deepak Kumar
 
 * http://www.roseindia.net HQL Group by Clause Example
 *  
 */
public class HQLGroupByExample {
  public static void main(String[] args) {
    Session session = null;
    try {
      // This step will read hibernate.cfg.xml and prepare hibernate for
      // use
      SessionFactory sessionFactory = new Configuration().configure()
          .buildSessionFactory();
      session = sessionFactory.openSession();
      //Group By Clause Example
      String SQL_QUERY = "select sum(insurance.investementAmount),insurance.insuranceName "
          "from Insurance insurance group by insurance.insuranceName";
      Query query = session.createQuery(SQL_QUERY);
      for (Iterator it = query.iterate(); it.hasNext();) {
        Object[] row = (Object[]) it.next();
        System.out.println("Invested Amount: " + row[0]);
        System.out.println("Insurance Name: " + row[1]);
      }
      session.close();
    catch (Exception e) {
      System.out.println(e.getMessage());
    finally {
    }
  }
}

轉載自http://zhangguyou2009.blog.163.com/blog/static/3469163820077221064764/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章