Sunday, November 14, 2010

Hibernate Criteria Query-AND Clause

Hibernate Criteria Query – Using AND clause.

Aim:
            To check more than one condition using AND clause.

Example:

Problem:

            To get student name who get tamil mark between greater than 50 and less than70.

Solution:

Step 1:
            Create Hibernate Reverse Engineering wizard

Step 2:
            Create HibernateUtil.java

Step 3:
            Create POJO class and mapping files

Step 4:

            Create jsp file named as AndExample.jsp

Include following coding into the AndExample.jsp

< %@page import="org.hibernate.*" % >
< %@page import="org.hibernate.cfg.*" % >
< %@page import="org.hibernate.criterion.*" % >
< %@page import="java.util.*" % >
< %@page import="Hiber.Student" % >
< html >
< head >
< title > Hibernate Criteria Query-And Example < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.and(Expression.gt("tamil",new Integer(50)), Expression.lt("tamil", new Integer(70))));
List list=c.list();
Iterator it=list.iterator();
while(it.hasNext())
{
Student s1=(Student)it.next();
out.println(s1.getStuname());
}
}
catch(Exception e)
{

}
% >
< /body >
< /html >


Output:


We have to check manually using mysql database using query.

mysql > select stuname from student where tamil > 50 and tamil < 70;

1 comment: