You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Do...@bedag.ch on 2005/08/18 15:08:34 UTC

Select n+1 Problem

Hi

Concerning the developer guide, it is possible to avoid the select n+1
problem
I have something like this:
Java:
Class Category{
	String name;
	Collection itemList;
	
	public void setItemList(Collection _itemList) {
		this.itemList = _itemList;
	}	
	public void setName(String _name){
		this.name=name;
	}
	...
}

Class Item{
	String name;
	public void setName(String _Name){
		this.name=_name;
	}	
	....
}

Database:
Table Category:
Id	name
1	Apple
2	Microsoft

Table Item:
Id	CategoryFk	name
1	1		OS X
2	1		iLife
3	2		whatever
4	2		worse whatever

Question:
I don't see how I can avoid to make for each category a select to the
item table.

(like this):
	List list = sqlMap.queryForList("getCategory",null);
	for (Iterator iter = list.iterator(); iter.hasNext();) {
		Category category=(Category)iter.next();
		List itemList = sqlMap.queryForList("getItem",new
Integer(category.getId));
		category.setItemList(itemList);
	}
I didn't fully understand  the Calendar sample in the developer guide. 
TIA Dominic