Home Answers Viewqa Hibernate AND operator in hibernate.

 
 


ratna rathor
AND operator in hibernate.
1 Answer(s)      11 months ago
Posted 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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.