Wednesday, August 08, 2007

HQL: select object(s) from query result set

When you select a particular column(s) from table using HQL, how do you handle the return result? what is the type of result will be returned?

Here is some example:
When you select a single object:
private List loadAffiliationsbyCategory(MucRoom room, int affiliation) {
return getHibernateTemplate().find("Select id.jid from MucAffiliation where id.room=? and affiliation=?",
new Object[] {room,affiliation});
}

Test code:
for(String key:loadAffiliationsbyCategory2(room, MucRole.Affiliation.outcast.getValue())){
log.debug("test " + key);
}


When you select more then one object:
private List loadAffiliationsbyCategory2(MucRoom room, int affiliation) {
return getHibernateTemplate().find("Select id.jid, affiliation from MucAffiliation where id.room=? and affiliation=?",
new Object[] {room,affiliation});
}

Test code:
for(Object[] key:loadAffiliationsbyCategory2(room, MucRole.Affiliation.outcast.getValue())){
log.debug("test " + (String)key[0] + (Integer)key[1]);
}

No comments: