package com.cv.hibernate.hql;
/** @author Chandra Vardhan */
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import com.cv.hibernate.hql.model.EmployeeBean;
import com.cv.hibernate.hql.util.HibernateUtil;
public class HQLListvsIterateProcessing {
private final static Logger logger = Logger
.getLogger(HQLListvsIterateProcessing.class);
public static void main(String args[]) throws Exception {
HibernateUtil.saveOrUpdate();
Session session = HibernateUtil.getSession();
Query query = session
.createQuery("select eb.id, eb.lastName, eb.email from EmployeeBean eb where eb.firstName in('chandra','KODAM')");
List list = query.list();
if (list != null && !list.isEmpty()) {
logger.info("Return type of the class : " + list.get(0).getClass());
for (int i = 0; i < list.size(); ++i) {
Object[] row = (Object[]) list.get(i);
String result = null;
for (int k = 0; k < row.length; k++) {
if (result == null) {
result = row[k].toString() + " ";
} else {
result += row[k].toString() + " ";
}
}
logger.info(result);
}
logger.info("---------- list() done ----------");
} else {
logger.info("No Records found for the specified input...");
}
Query query2 = session
.createQuery("select eb.id, eb.lastName, eb.email from EmployeeBean eb where eb.firstName in('chandra','KODAM')");
Iterator it = query2.iterate();
if (it != null) {
while (it.hasNext()) {
Object[] row = (Object[]) it.next();
String result = null;
for (int k = 0; k < row.length; k++) {
if (result == null) {
result = row[k].toString() + " ";
} else {
result += row[k].toString() + " ";
}
}
logger.info(result);
}
} else {
logger.info("No Records found for the specified input...");
}
session.close();
}
} |
No comments:
Post a Comment