package com.cv.hibernate.filter;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Filter;
import org.hibernate.Query;
import org.hibernate.Session;
import com.cv.hibernate.filter.model.EmployeeBean;
import com.cv.hibernate.filter.util.HibernateUtil;
/**
* @author Chandra Vardhan
*/
public class FilterTest {
private final static Logger logger = Logger.getLogger(FilterTest.class);
public static void main(String args[]) throws Exception {
HibernateUtil.saveOrUpdate();
Session session = HibernateUtil.getSession();
Filter filter = session.enableFilter("hibernateFilterInPostgres");
filter.setParameter("p1", 100);
filter.setParameter("p2", 103);
Query query = session.createQuery("from EmployeeBean");
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) {
EmployeeBean eb = (EmployeeBean) list.get(i);
logger.info(eb.getId() + " " + eb.getFirstName() + " "
+ eb.getLastName() + " " + eb.getEmail());
}
} else {
logger.info("No Records found for the specified input...");
}
Query query2 = session.getNamedQuery("SQLSelectQueryByID");
query2.setInteger("p1", 104);
List list2 = query2.list();
if (list != null && !list.isEmpty()) {
logger.info("Return type of the class : " + list.get(0).getClass());
for (int i = 0; i < list2.size(); ++i) {
EmployeeBean eb4 = (EmployeeBean) list2.get(i);
logger.info(eb4.getId() + " " + eb4.getFirstName() + " "
+ eb4.getLastName() + " " + eb4.getEmail());
}
} else {
logger.info("No Records found for the specified input...");
}
session.disableFilter("hibernateFilterInPostgres");
Query q2 = session.createQuery("select count(*) from EmployeeBean");
list = q2.list();
logger.info("hello===count of rcords is=====" + list.toString());
HibernateUtil.closeSession();
}
} |
No comments:
Post a Comment