How to use OR operator in hibernate?
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(); System.out.println(employee.getName()); } session.close(); } }
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.