Sunday, November 14, 2010

Hibernate Criteria Query-GreaterThanEqual

Hibernate Criteria Expression- greater than and Equal


Aim:
            To learn how to use greater than and Equal in our hibernate.

Problem:

To get stuname from table student in navalady database who get greater than and Equal 70 marks in tamil using greater than and Equal criteria expression.

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 GreaterEqual.jsp

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

Criteria c=s.createCriteria(Student.class);
c.add(Expression.ge("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)
{
out.println(e);
}
% >
< /body >
< /html >


Output:



To check using mysql using following query

mysql > select stuname from student where tamil > =70;

Note:
All example programs in our blog are successfully run. So you copy program and paste to your IDE and run for your experience.

No comments:

Post a Comment