|
Where not in Clause
The following example shows using HQL where not in clause.
Refer first example for the configuration and mapping.
|
package com.bethecoder.tutorials.hibernate.basic.tests;
import java.util.List;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;
public class HQLWhereNotInClauseTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Where in clause with company ids");
getCompanies("from Company comp where comp.id not in (1, 2, 3, 4, 5, 6)");
System.out.println("Where in clause with company names");
getCompanies("from Company comp where comp.name not in ('ABC', 'PQR', 'MNO', 'IJK')");
}
private static void getCompanies(String hql) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List<Company> companies = (List<Company>) session.createQuery(hql).list();
for (Company company : companies) {
System.out.println(company);
}
session.getTransaction().commit();
session.close();
}
}
|
| |
It gives the following output,
Where in clause with company ids
{ id = 7, name = AOQ, employeeCount = 97890,
projectCount = 4578, address1 = add17892, address2 = add2893 }
{ id = 8, name = BQO, employeeCount = 9778,
projectCount = 487, address1 = add1459, address2 = add2443 }
{ id = 9, name = CFR, employeeCount = 478,
projectCount = 2487, address1 = null, address2 = add2443 }
{ id = 10, name = CFR, employeeCount = 6478,
projectCount = 887, address1 = , address2 = add24435 }
Where in clause with company names
{ id = 5, name = ART, employeeCount = 67890,
projectCount = 850, address1 = add1112, address2 = add2223 }
{ id = 6, name = RNK, employeeCount = 98890,
projectCount = 8478, address1 = add1412, address2 = add27823 }
{ id = 7, name = AOQ, employeeCount = 97890,
projectCount = 4578, address1 = add17892, address2 = add2893 }
{ id = 8, name = BQO, employeeCount = 9778,
projectCount = 487, address1 = add1459, address2 = add2443 }
{ id = 9, name = CFR, employeeCount = 478,
projectCount = 2487, address1 = null, address2 = add2443 }
{ id = 10, name = CFR, employeeCount = 6478,
projectCount = 887, address1 = , address2 = add24435 }
|
|