Sunday, November 14, 2010

Hibernate Criteria Expression- lessthanequal

Hibernate Criteria Expression- lessthanequal


Aim:
            To learn how to use lessthanequal in our hibernate.

Problem:

To get stuname from table student in navalady database who get less than and equal 77 marks in tamil using lessthan 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 LessThanEq.jsp

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

Criteria c=s.createCriteria(Student.class);
c.add(Expression.le("tamil",new Integer("77")));

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 manually check using mysql query.

mysql > select stuname from student where tamil < =77;

No comments:

Post a Comment