Last Updated: February 25, 2016
·
2.643K
· rajeshpg

Construct Hibernate Criteria Query with OR clause

public List<Employee> getEmployees(final Set<String> employeeCodes)
{
List<Employee> employees = (List<Employee>) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(final Session session) throws HibernateException, SQLException
{
{
final Criteria criteria = session.createCriteria(Employee.class);

            if(employeeCodes.size() > 1000){

                final Set<String> codes = new HashSet<String>();
                Disjunction disjuntion = Restrictions.disjunction();

                for(Iterator<String> iterator = employeeCodes.iterator(); iterator.hasNext();){

                    if(codes.size() < 1000){
                        codes.add(iterator.next());
                    }else{
                        criteria.add(disjuntion.add(Restrictions.in("employeeCode",codes)));
                        codes.clear();
                    }
                }
            }else{
                criteria.add(Restrictions.in("employeeCode",employeeCodes));
            }

               return criteria.list();
        }
    }
});
return employees;

}