AND operator in hibernate.

AND operator in hibernate.

How to use AND operator in hibernate?

View Answers

May 31, 2012 at 11:39 AM

AND operator is used for checking all the conditions. If all the conditions satisfied then only it will return the result 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 HibernateANDOperator{
    public static void main(String []args){
        SessionFactory sessionFactory = ConnectionUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Employee.class);
                criteria.add(Restrictions.and(Restrictions.eq("id",1), 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 AND operator returning the result after checking both the conditions. First one to check equality of id must be 1 and the second one to check the name must be start with ?r? character.









Related Tutorials/Questions & Answers:

Ads