You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ibatis.apache.org by Kris Jenkins <kr...@yahoo.co.uk> on 2005/01/19 13:34:04 UTC

Suppress empty beans on grouping

Hey all,

I've just been playing around with 2.0.9's groupBy feature, which looks 
really groovy.  Incredibly groovy in fact - I'm stripping out quite a 
lot of old, horrible code atm thanks to it.

But, I do have one question...

I've got a query that does something like this - fetch all customers, 
and the amount they've spent on each product they've bought:

SELECT c.customer_name, o.product_name, sum( o.cost )
FROM
    order o,
    customer c
WHERE c.custormer_id = o.customer_id *(+)*  -- Note the outer join.
GROUP BY c.customer_name, o.product_name

This returns a result set like:

CUSTOMER_NAME  | PRODUCT_NAME | COST
---------------+--------------+------
BOB            | CATS         | $135
BOB            | DOGS         | $35
JIM            | APES         | $200
STOKER         | null         | null
XAVIER         | CHIMPS       | $50

So that in the bean graph:
    customers.get( 0 ) == "BOB"
    customers.get( 0 ).getProducts().size() == 2

Which is exactly what I want.  Unfortunately:

    customers.get( 2 ) == "STOKER"  // Good
    customers.get( 1 ).getProducts().size() == 1 // Bad

Stoker ends up with a product bean that has been zero-arg constructed, 
but never set.  What 'd really like is either Stoker.getProducts() to 
return null or (preferably) an empty list.  Is there/will there be a way 
to suppress 'empty' beans?  For now I'll drop them from the bean logic, 
but it would be nice not to have to.

Cheers,
Kris


-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.0 - Release Date: 17/01/2005