Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String HQL_QUERY = "from Company comp where 5 > " +
"(select count(*) from Company comp2 where comp2.employeeCount < comp.employeeCount)" +
" order by comp.employeeCount";
List<Company> companies = (List<Company>) session.createQuery(HQL_QUERY).list();
for (Company company : companies) {
System.out.println(company);
}