ratna rathor
OR operator in hibernate.
1 Answer(s)      3 years and 6 months ago
Posted in : Hibernate

How to use OR operator in hibernate?

View Answers

May 31, 2012 at 11:40 AM

OR operator is used for checking all the conditions. If any one of them is satisfied then it will return the results according to the conditions. Here is your example ?

package net.roseindia.main;
import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.ConnectionUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

public class HibernateOROperator{
    public static void main(String []args){
        SessionFactory sessionFactory = ConnectionUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Employee.class);

        criteria.add(Restrictions.or(Restrictions.eq("id",3), Restrictions.like("name", "R%")));
         List<Employee> employeeList = new ArrayList<Employee>();
        employeeList = criteria.list();
        Iterator it = employeeList.iterator();
        while (it.hasNext()) {
            Employee employee = (Employee) it.next();

Description: In the above example OR operator returning the result if any one expression is true. First one to check equality of id must be 3 and the second one to check the name must be started with ?r? character.

Related Tutorials/Questions & Answers: