2010年10月4日 星期一

What if the hql query is a row return ?

Sometimes we want to select several tables using join in hql, however, the result of the selection maybe is not defined in your data model. When encounter this kinds of situation, we will need the following code to solve it.
The selection data will stay in a record set using a for loop to retrive the fields and row.

Query query = session.createQuery(SQL_QUERY);
for(Iterator it=query.iterate();it.hasNext();){
Object[] row = (Object[]) it.next();
System.out.println("ID: " + row[0]);
System.out.println("Name: " + row[1]);
System.out.println("Amount: " + row[2]);
}

參考來源:

"Query query = session.createQuery(SQL_QUERY);  for(Iterator it=query.iterate();it.hasNext();){        Object[] row = (Object[]) it.next();        System.out.println("ID: " + row[0]);        System.out.println("Name: " + row[1]);        System.out.println("Amount: " + row[2]);      }"
- Hibernate Select Clause (在「Google 網頁註解」中檢視)

沒有留言: