Monday, November 15, 2010

Hibernate Criteria Query-OR Clause

Hibernate Criteria Query – Using OR clause.

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

Example:

Problem:

            To get student name who get tamil mark between greater than 50 or 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 OrExample.jsp

Include following coding into the OrExample.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-Or Example < /title >
< /head >
< body >
< %
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session s=sessionFactory.openSession();

Criteria c=s.createCriteria(Student.class);
c.add(Expression.or(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










To check using mysql database.

Using following mysql query

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

No comments:

Post a Comment