Archive for September 18th, 2007

Hibernate free queries

Let hibernate execute a reale SQL statement

List travelerTypeList = hs.createQuery("SELECT id, locale, type FROM Utypes WHERE locale = '"en"' AND isdefault = 1 ORDER BY type ASC").list();
 
if (!travelerTypeList.isEmpty()) {
	Iterator tt_iter = travelerTypeList.iterator(); 
	while (tt_iter.hasNext()) { 
		Object[] tmp_obj = (Object[])tt_iter.next();
		String tmp_id = tmp_obj[0];
		String tmp_locale = tmp_obj[1];
		String tmp_type = tmp_obj[2]
	}
}
 
 
import org.hibernate.*;
List travelerTypeList = hs.createSQLQuery("SELECT id, type, 1 AS link FROM utypes, user_utypes WHERE id = utype_id AND user_id = "+usersBean.getId())
				.addScalar("id", Hibernate.INTEGER)
				.addScalar("type", Hibernate.STRING)
				.addScalar("link", Hibernate.INTEGER)
				.list();
 
if (!travelerTypeList.isEmpty()) {
	Iterator tt_iter = travelerTypeList.iterator(); 
	while (tt_iter.hasNext()) { 
		Object[] tmp_obj = (Object[])tt_iter.next();
		Integer tmp_id = tmp_obj[0];
		String tmp_type = tmp_obj[1];
		Integer tmp_link = tmp_obj[2]
	}
}
 
 
List travelerTypeList = hs.createSQLQuery("SELECT id AS id0_, type AS type4_0_ FROM cities WHERE region_id = "+selectedRegionId)
				.addEntity("cities", Cities.class)
				.list();
 
if (!travelerTypeList.isEmpty()) {
	Iterator tt_iter = travelerTypeList.iterator(); 
	while (tt_iter.hasNext()) { 
		Utypes tmp_utype = (Utyps)tt_iter.next();
		Integer tmp_id = tmp_utype.getId();
		String tmp_type = tmp_utype.getType();
	}
}
 
 
List travelerTypeList = hs.createQuery("from Utypes WHERE type = 'Single' ORDER BY type ASC").list();
 
if (!travelerTypeList.isEmpty()) {
	Iterator tt_iter = travelerTypeList.iterator(); 
	while (tt_iter.hasNext()) { 
		Utypes tmp_utype = (Utyps)tt_iter.next();
		Integer tmp_id = tmp_utype.getId();
		String tmp_type = tmp_utype.getType();
	}
}