Wednesday, November 17, 2010

Hibernate Projection Example.


Hibernate Projection Example.

Introduction:

Problem:

            To get stuname and tamil mark from table(Student) in database(Navalady).

Database:




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

            Include following coding into ProjectionExample.jsp


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

Criteria crit=se.createCriteria(Student.class);

ProjectionList p=Projections.projectionList();
p.add(Projections.property("stuname"));
p.add(Projections.property("tamil"));

crit.setProjection(p);

List list=crit.list();
Iterator it=list.iterator();
% >
< table border="3" >
< tr >
< td > Student Name < /td >
< td > Tamil Mark < /td >
< /tr >
< %
while(it.hasNext())
{
Object[] obj=(Object[])it.next();
out.println("< tr > < td > ");
out.println(obj[0]);
out.println(" < /td > < td > ");
out.println(obj[1]);
out.println(" < /td > < /tr > ");
}
% >
< /table >
< %
}
catch(Exception e)
{
out.println(e);
}
% >
< /body >
< /html >


Output

No comments:

Post a Comment