package com.cv.hibernate.hql;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.apache.log4j.Logger;
import com.cv.hibernate.hql.util.HibernateUtil;
public class HQLFindAll {
/**
* @param args
*/
private final static Logger logger = Logger.getLogger(HQLFindAll.class);
public static void main(String[] args) {
// Am creating the recordords by this statement.
HibernateUtil.saveOrUpdate();
Session session = HibernateUtil.getSession();
Query q1 = session
.createQuery("select count(*),min(eb.id),max(eb.id),avg(eb.id),sum(eb.id) from EmployeeBean eb ");
List list = q1.list();
if (list != null && !list.isEmpty()) {
logger.info("Return type of the class : " + list.get(0).getClass());
Object[] obj = (Object[]) list.get(0);
logger.info("====================================");
logger.info("Return type of count(*) : " + obj[0].getClass());
logger.info("Return type of min : " + obj[1].getClass());
logger.info("Return type of max : " + obj[2].getClass());
logger.info("Return type of avg : " + obj[3].getClass());
logger.info("Return type of sum : " + obj[4].getClass());
logger.info("====================================");
// long cnt=((Long)obj[0]).longValue();
int cnt = Integer.parseInt(obj[0].toString());// eventhough it is a
// long
// it is also
// working
// with int
// datatype.
int min = Integer.parseInt(obj[1].toString());
int max = Integer.parseInt(obj[2].toString());
double avg = ((Double) obj[3]).doubleValue();
// float avg=((Float)obj[3]).floatValue();//here it will not working
int sum = Integer.parseInt(obj[4].toString());
logger.info("count of record===" + cnt);
logger.info("min of record eid===" + min);
logger.info("max of record eid===" + max);
logger.info("avg of record eid===" + avg);
logger.info("sum of record eid===" + sum);
} else {
logger.info("No Records found for the specified input...");
}
HibernateUtil.closeSession();
}
} |
No comments:
Post a Comment