|
Conjunction Criteria
The following example shows how to use Hibernate Conjunction criteria.
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.Conjunction;
import org.hibernate.criterion.Restrictions;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class CriteriaConjunctionRestrictionTest {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Company.class);
Conjunction conjunction = Restrictions.conjunction();
conjunction.add(Restrictions.eq("id", new Integer(2)));
conjunction.add(Restrictions.eq("name", "ABC"));
conjunction.add(Restrictions.eq("employeeCount", new Integer(23430)));
criteria.add(conjunction);
List<Company> companies = criteria.list();
System.out.println("Selected row count : " + companies.size());
for (Company company : companies) {
System.out.println(company);
}
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Selected row count : 1
{ id = 2, name = ABC, employeeCount = 23430,
projectCount = 160, address1 = add11, address2 = add22 }
|
|