|
Null Restriction
The following example shows how to use Hibernate Null 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 CriteriaNullRestrictionTest {
/**
* @param args
*/
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Company.class);
criteria.add(Restrictions.isNull("address1"));
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 = 9, name = CFR, employeeCount = 478,
projectCount = 2487, address1 = null, address2 = add2443 }
|
|