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

Case Insensitive Search 

The following example shows case insensitive search using HQL. Refer first example for the configuration and mapping. Here :company_name represents named parameter which will be substituted with value set using query.setParameter method.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/tests/HQLCaseInSensitiveTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.hibernate.basic.tests;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class HQLCaseInSensitiveTest {

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

    System.out.println("Companies name like 'aB'");
    getCompanies("aB");
    
    System.out.println("Companies name like 'aRt'");
    getCompanies("aRt");
    
    System.out.println("Companies name like 'rn'");
    getCompanies("rn");
  }
  
  private static void getCompanies(String companyName) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String HQL_QUERY = "from Company comp where upper(comp.name) like :company_name";
    Query query = session.createQuery(HQL_QUERY);
    query.setParameter("company_name""%" + companyName.toUpperCase() "%");
    
    List<Company> companies = query.list();    
    
    for (Company company : companies) {
      System.out.println(company);
    }
    
    session.getTransaction().commit();
    session.close();
  }

}
   

It gives the following output,
Companies name like 'aB'
{ id = 2, name = ABC, employeeCount = 23430, 
	projectCount = 160, address1 = add11, address2 = add22 }
Companies name like 'aRt'
{ id = 5, name = ART, employeeCount = 67890, 
	projectCount = 850, address1 = add1112, address2 = add2223 }
Companies name like 'rn'
{ id = 6, name = RNK, employeeCount = 98890, 
	projectCount = 8478, address1 = add1412, address2 = add27823 }


                    Print

   



Books Recommendation

 
 
bl  br