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 Yannick Le Teigner <ya...@laposte.net> on 2006/01/14 15:41:13 UTC

ResultMap, N+1

Hi,

I have a problem with a resultMap.

My iBatis is:

  <resultMap id="subcategoriesMap" class="Subcategory" groupBy="id">
    <result property="id" column="id" />
    <result property="categoryId" column="cid" />
    <result property="category" column="cname" />
    <result property="name" column="sname" />
    <result property="acc" resultMap="Billing.account"/>
  </resultMap>

  <resultMap id="account" class="LedgerAccount">
    <result property="id" column="aid"/>
    <result property="name" column="aname"/>
    <result property="number" column="anumber"/>
  </resultMap>

  <select id="getLedgerAccountSubcategories" resultMap="subcategoriesMap">
    SELECT ledger_account_subcategories.id as id, 
ledger_account_categories.id as cid, ledger_account_categories.name as 
cname, ledger_account_subcategories.name as sname, ledger_accounts.id as 
aid, ledger_accounts.name as aname, ledger_accounts.number as anumber
    FROM ledger_account_subcategories
    LEFT JOIN ledger_account_categories ON 
category_id=ledger_account_categories.id
    LEFT JOIN ledger_accounts_to_subcategories ON 
ledger_account_subcategories.id=subcategory_id
    LEFT JOIN ledger_accounts ON account_id=ledger_accounts.id;
  </select>

The query returns below results:

 id | cid |         cname          |    sname    | aid |          
aname           | anumber
----+-----+------------------------+-------------+-----+--------------------------+---------
  2 |   4 | Produits Exeptionnels  | Subventions |  12 | Cpte Associé - 
C. PERRIN |   45614
  2 |   4 | Produits Exeptionnels  | Subventions |  22 | Cpte 
MCC                 |   41101
  3 |   3 | Charges Financières    | Taxes       |     
|                          |       
  4 |   1 | Revenues               | Ventes      |     
|                          |       
  5 |   2 | Charges d'Exploitation | Lab         |     
|                          |       


The problem is that the main object (Subcategory) gets correctly created 
(4 times), and that the LedgerAccount "sub-object" gets also correctly 
created for Subcategory #1 (with id=2), but it does not get associated 
with its parent Subcategory.

Here are some traces:

Subcategory Constructor
id = 2
setAcc: null
setAcc: []
  new account
  new account

Subcategory Constructor
id = 3
setAcc: null
setAcc: []

Subcategory Constructor
id = 4
setAcc: null
setAcc: []

Subcategory Constructor
id = 5
setAcc: null
setAcc: []


You can see that for each object is created correctly, but that the 
LedgerAccount created for the 1st object is created -after- the call to 
Subcategory.setAcc(), and does not get associated with it.
acc is defined as a List in Subcategory.


Thanks for any help!
Yannick