|
Between Restriction
The following example shows how to use Hibernate between restriction.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.criteria;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class CriteriaBetweenRestrictionTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Companies having emp count between (2000-12000)");
getCompanies(2000, 12000);
System.out.println("Companies having emp count between (20000-60000)");
getCompanies(20000, 60000);
}
private static void getCompanies(int empMinCount, int empMaxCount) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Company.class);
criteria.add(Restrictions.between("employeeCount", empMinCount, empMaxCount));
List<Company> companies = criteria.list();
for (Company company : companies) {
System.out.println(company);
}
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Companies having emp count between (2000-12000)
{ id = 1, name = PQR, employeeCount = 3430,
projectCount = 220, address1 = add1@, address2 = add2 }
{ id = 8, name = BQO, employeeCount = 9778,
projectCount = 487, address1 = add1459, address2 = add2443 }
{ id = 10, name = CFR, employeeCount = 6478,
projectCount = 887, address1 = , address2 = add24435 }
Companies having emp count between (20000-60000)
{ id = 2, name = ABC, employeeCount = 23430,
projectCount = 160, address1 = add11, address2 = add22 }
{ id = 3, name = MNO, employeeCount = 26790,
projectCount = 670, address1 = add111, address2 = add222 }
|
|