tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Hibernate > Hibernate Query Language > Like Clause

Like Clause 

The following example shows using HQL like clause. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLLikeClauseTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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 HQLLikeClauseTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    System.out.println("Companies name like '%A%'");
    getCompanies("from Company comp where comp.name like '%A%'");
    
    System.out.println("Companies name like '%Q%'");
    getCompanies("from Company comp where comp.name like '%Q%'");
  }
  
  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,
Companies name like '%A%'
{ id = 2, name = ABC, employeeCount = 23430, 
	projectCount = 160, address1 = add11, address2 = add22 }
{ id = 5, name = ART, employeeCount = 67890, 
	projectCount = 850, address1 = add1112, address2 = add2223 }
{ id = 7, name = AOQ, employeeCount = 97890, 
	projectCount = 4578, address1 = add17892, address2 = add2893 }

Companies name like '%Q%'
{ id = 1, name = PQR, employeeCount = 3430, 
	projectCount = 220, address1 = add1@, address2 = add2 }
{ id = 7, name = AOQ, employeeCount = 97890, 
	projectCount = 4578, address1 = add17892, address2 = add2893 }
{ id = 8, name = BQO, employeeCount = 9778, 
	projectCount = 487, address1 = add1459, address2 = add2443 }



 
  


  
bl  br