How to use Native SQL?



SQLQuery query = session.createSQLQuery(" select * from ENTITIES_DATA ");
// Here ENTITIES_DATA is the table in the database

List list = query.list();
Iterator iterator = list.iterator();
while(iterator.hasNext())
{
  Object row[] = (Object[])iterator.next();
  /*----- further code--------*/
}

while selecting data from the table, even though you are selecting the complete object from
the table, in while loop still we type cast into object array only right

See the above code, we typecast into the object[] arrays, in case if we want to type cast into
our POJO class (i mean to get POJO class obj), then we need to go with entityQuery concept

In order to inform the hibernate that convert each row of ResultSet into an object of the POJO class back, we need to make the query as an entityQuery

To make the query as an entityQuery, we need to call addEntity() method

How to use addEntity() ?
//We are letting hibernate to know our POJO class too
SQLQuery qry = session.createSQLQuery("select * from ENTITIES_DATA").addEntity(Entity.class);
List l = qry.list();

Iterator it = l.iterator();
while(it.hasNext())
{
    Entity p = (Entity)it.next();    

}