Succinctest way of using Complex Collection Properties ?

9 views
Skip to first unread message

fxbird

unread,
Aug 14, 2010, 9:17:33 AM8/14/10
to mybatis-user
Hi, all:
 
   what's the Succinctest way of using Complex Collection Properties ? 
 
   As we know ,when we do a 1:1 query , a bean has a complex bean property for instance a Product has a Category property, we can write the select statement query a product and its category like below:
 
    <select id="getProductWithCategory"  resultClass="product"  parameterClass="int">
        select
           p.prd_id pid,
           p.prd_name name,
           p.prd_description description,
           c.cat_id as "category.cid",
           c.cat_name as "category.name"
        from product p,category c
         where p.prd_id=#pid# and p.cat_id=c.cat_id
    </select>
 
    that is , we don't need do as guide doc says, define two resultMap for that, just use dot referring to the relationship. How succinct , graceful it is!  But how about Complex Collection Properties  , how about a Category has a list property referring to all the products related?
    To query a category and its product list , I have to do fully according to what guide says,define two resultMap as below, it does work, but a little boring. Is there better , succint way to it?
 
 <typeAlias alias="product" type="xdg.model.Product"/>
    <typeAlias alias="category" type="xdg.model.Category"/>
   <resultMap id="productList" class="product">
       <result property="pid" column="prd_id"/>
       <result property="name" column="prd_name"/>
   </resultMap>
   <resultMap id="categoryResult" class="category" groupBy="cid">
       <result property="cid" column="cat_id"/>
       <result property="name"  column="cat_name"/>
       <result property="products" resultMap="category.productList"/>
   </resultMap>
    <select  id="getCategoryList" resultClass="category">
        select *  
        from category c,product p
        where c.cat_id=p.cat_id
        order by c.cat_id;
    </select>
 
 
  thank so much.
 
Kurt
2010-08-14

fxbird

Poitras Christian

unread,
Aug 16, 2010, 9:04:20 AM8/16/10
to mybati...@googlegroups.com
From what I see you are using iBATIS 2. In this case I think it is the way to go.
In MyBatis 3, you can nest a resultMap inside a <collection> element.
 
Christian


From: mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] On Behalf Of fxbird
Sent: Saturday, August 14, 2010 9:18 AM
To: mybatis-user
Subject: Succinctest way of using Complex Collection Properties ?

fx bird

unread,
Aug 16, 2010, 10:46:48 AM8/16/10
to mybati...@googlegroups.com
Hi Christian:
   You mean it's only way in ibatis2?

    thanks.

Kurt

Poitras Christian

unread,
Aug 16, 2010, 10:51:39 AM8/16/10
to mybati...@googlegroups.com
I am sorry, I don't really use iBATIS 2 anymore.
 
After a quick look, you can use groupBy in your resultMap.
   <resultMap id="categoryResult" class="category" groupBy="cid">
       <result property="cid" column="cat_id"/>
       <result property="name"  column="cat_name"/>
       <result property="products.pidcolumn="prd_id"/>
       <result property="products.namecolumn="prd_name"/>
   </resultMap>
 
Christian

From: mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] On Behalf Of fx bird
Sent: Monday, August 16, 2010 10:47 AM
To: mybati...@googlegroups.com
Subject: Re: Succinctest way of using Complex Collection Properties ?

Reply all
Reply to author
Forward
0 new messages