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 Perry Nguyen <pf...@hanhuy.com> on 2005/12/17 00:43:05 UTC
N+1 solution and queryForList(String, Object, int, MAX)
When using the iBATIS N+1 solution and the max parameter in
queryForList, the behavior is a little bit different than what I would
expect.
The max parameter appears to only address the total rows returned,
however and not the number of elements that would be populated into the
return List.
E.g.
I have a table:
products (
id
name
)
product_attributes (
prod_id,
name,
value
)
With a query like:
select p.id, p.name, a.name, a.value
from products p, product_attributes a
If there are 30 attributes to a single product and I specify the max
results to only be 15, I don't get 15 product objects in my List, but
rather a single product object with 15 attributes populated.
This behavior is quite misleading as I would expect to retrieve 15
product objects. Is there any workaround to this? Does anything depend
on the existing behavior? Can it be changed to support my desired behavior?
Re: N+1 solution and queryForList(String, Object, int, MAX)
Posted by Perry Nguyen <pf...@hanhuy.com>.
Ok,
It looks like I can handle this situation with a RowHandler, but how can
I implement the MAX condition within a RowHandler as it seems the
interface does not offer any way to stop parsing the result set.
I currently have a workaround in which I simply throw a RuntimeException
from within my handler and catch the SQLException that
queryWithRowHandler throws.
This works, for now, but it seems like such an ugly solution... Is
there an "approved" method of stopping a RowHandler?
Perry Nguyen wrote:
> When using the iBATIS N+1 solution and the max parameter in
> queryForList, the behavior is a little bit different than what I would
> expect.
>
> The max parameter appears to only address the total rows returned,
> however and not the number of elements that would be populated into the
> return List.
>
> E.g.
>
> I have a table:
>
> products (
> id
> name
> )
>
> product_attributes (
> prod_id,
> name,
> value
> )
>
> With a query like:
>
> select p.id, p.name, a.name, a.value
> from products p, product_attributes a
>
> If there are 30 attributes to a single product and I specify the max
> results to only be 15, I don't get 15 product objects in my List, but
> rather a single product object with 15 attributes populated.
>
> This behavior is quite misleading as I would expect to retrieve 15
> product objects. Is there any workaround to this? Does anything depend
> on the existing behavior? Can it be changed to support my desired
> behavior?