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",
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