Hibernate Restriction Like MatchMode Exact
In this section, you will learn about Hibernate restriction like() or Restrictions.like() MatchMode.EXACT function with running example, code and its related output. Here, we will see the hibernate restrictions like () method description and its running example.
Restrictions.like("field_name", "like_query_string", MatchMode.EXACT): Hibernate Restrictions.like() function takes three parameters:
- field_name: Which you want to match like_query_string.
- like_query_string: This is user matching string which you want to match.
- MatchMode.EXACT: Like function match the query string according to MatchMode. Here we will see about the MatchMode.EXACT. MatchMode EXACT matches the value exactally to the like_query_string.
Now, We will see the running example of Restrictions.llike() or like() function with MatchMode.EXACT. The following example match the field_name = "empName", like_query_string = "Sujeet Jha " and MatchMode.EXACT. It gives you all results which having empName is "Sujeet Jha" exactally.
package developerhelpway.hibernate.criteria.restriction;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import developerhelpway.hibernate.Employee;
public class RestrictionLikeMatchModeExactExample {
/**
* @param args
*/
public static void main(String[] args) {
Session sess = null;
try{
SessionFactory sf = new Configuration().
configure().buildSessionFactory();
sess = sf.openSession();
Transaction tr = sess.beginTransaction();
Criteria criteria = sess.createCriteria(
Employee.class);
String matchString = "Sujeet Jha";
criteria.add(Restrictions.like(
"empName", matchString, MatchMode.EXACT));
List<Employee> employees = criteria.list();
System.out.println("Get Employee which
exactally match with :" + matchString);
for(Employee emp: employees){
System.out.println(
"Id: " + emp.getEmpId()
+ ", EmpName: " + emp.getEmpName()
+ ", EmpSal: " + emp.getEmpSal()
);
}
tr.commit();
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(sess != null){
sess.close();
}
}
}
}
|
Download Restriction like function MatchMode Exact Example
After running this example you will see the following output and SQL query which is generated by hibernate:
Hibernate: select this_.emp_id as emp1_0_0_, this_.emp_name as emp2_0_0_, this_.emp_sal as emp3_0_0_, this_.emp_expences as emp4_0_0_ from employee this_ where this_.emp_name like ?
OutPut:
