Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Hibernate AND operator in hibernate.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ratna rathor
AND operator in hibernate.
1 Answer(s)      2 years and 7 months ago
Posted in : Hibernate


How to use AND operator in hibernate?


Advertisement
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:
 

 

 

DMCA.com