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 Peter Lee <le...@hotmail.com> on 2007/09/24 18:03:28 UTC

Errors populating object graph, multiple equal objects



I am running into an issue populating an object hierarchy
that look like this:



Groups have Members

               
           Members have Accounts

                 
         Members have Dependents





Essentially, I am either getting multiple Accounts or multiple Dependents,
depending on which one comes first in the SqlMap  (I switched the order to
verify).  It is always the number of Dependents (or accounts) times the
number of Accounts (or Dependents).  If I take out either Accounts or
Dependents, everything populates just fine.  As an example, for one
member, if I have 2 accounts and two dependents, I end up with 2 accounts and 4
dependents (with the original 2 each appearing twice).



I am not sure if this is not supported, I am mapping something wrong, or
otherwise.  Any help would be appreciated.  I am using version
2.3.0.677.



I did debug into the Ibatis code and noticed that, in BasicResultMap class
(line 371) that when checking for a unique key, it concatenates all of the keys
together ("ukey") which I am assuming is anything specified with a
"groupBy" although I had a little trouble verifying that.  If my
understanding of the code is correct, this would lead to this situation:



GroupId: %

Member Id: 1

Account Ids :  A and B

Dependent Ids  X and Y



By the time you are populating the last list,  the same dependent would
get populated twice with the following concatenated keys:



%1AX and %1BX



X represents the same Dependent, but, because the key is not found in the map,
it is assumed to be a new Dependent and added to the result list.  
As previously mentioned, if I switch the order of accounts and dependents, 
I end up with too many accounts, so I do not think it is the groupBy values or
the mapping.



Am I way off base?  Any thoughts? Any help would be much appreciated.





Here is some of the involved SqlMaps:



----  Group,xml



<resultMap id="group" class="domain.member.Group"
groupBy="groupId">

    <result property="groupId" 
            
column="gr_key"/>

    ...

</resultMap>



<resultMap id="groupWithMembersWithDependentsAndAccounts"
class="domain.member.Group" extends="group">

        <result property="members"  
resultMap="Member.memberWithAccountsAndDependents"/>

</resultMap>



<select id = "getGroupsWithMembersWithAccountsWithDependents" 

   
resultMap="groupWithMembersWithDependentsAndAccounts">

    

    SELECT

    <include refid="SqlFragment.memberFieldFragment"/>,

    <include
refid="SqlFragment.groupFieldFragment"/>,

    <include
refid="SqlFragment.dependentFieldFragment"/>,

    <include
refid="SqlFragment.accountFieldFragment"/>

    FROM group, member, depend, account

    WHERE 

        gr_key == mb_gr_key

        AND mb_key == dp_mb_key

        AND mb_key == ms_mb_key

</select>



----  Member.xml



<resultMap id="member" class="domain.member.Member"
groupBy="id">

    <result property="id"    
               
    column="mb_key"/>

    ...

</resultMap>



<resultMap id="memberWithAccountsAndDependents"
class="domain.member.Member" 

    extends="member">

        <result
property="dependents" resultMap="Dependent.dependent"/>

        <result
property="accounts"  
resultMap="Account.account"/>

</resultMap>



----  Account.xml



<!-- Result Maps -->

<resultMap id="account"
class="domain.member.Account"  groupBy="accountId">

    <result
property="accountId"                 
column="ms_key"/>

    ...

</resultMap>



---- Dependent.xml



<!-- Result Maps -->

<resultMap id="dependent"
class="domain.member.Dependent" groupBy="dependentId">

    <result property="dependentId"
column="dp_key"/>

    ...

</resultMap>


_________________________________________________________________
Gear up for Halo® 3 with free downloads and an exclusive offer. It’s our way of saying thanks for using Windows Live™.
http://gethalo3gear.com?ocid=SeptemberWLHalo3_WLHMTxt_2

Re: Errors populating object graph, multiple equal objects

Posted by Jeff Butler <je...@gmail.com>.
Multiple independant lists are not currently supported in iBATIS.  As you've
noticed, you'll have to take out either Accounts or Dependants for this to
work correctly, then populate the other list with a seperate select.

The issue is noted in JIRA here:

http://issues.apache.org/jira/browse/IBATIS-396

Jeff Butler



On 9/24/07, Peter Lee <le...@hotmail.com> wrote:
>
>  I am running into an issue populating an object hierarchy that look like
> this:
>
> Groups have Members
>                            Members have Accounts
>                            Members have Dependents
>
>
> Essentially, I am either getting multiple Accounts or multiple Dependents,
> depending on which one comes first in the SqlMap  (I switched the order to
> verify).  It is always the number of Dependents (or accounts) times the
> number of Accounts (or Dependents).  If I take out either Accounts or
> Dependents, everything populates just fine.  As an example, for one member,
> if I have 2 accounts and two dependents, I end up with 2 accounts and 4
> dependents (with the original 2 each appearing twice).
>
> I am not sure if this is not supported, I am mapping something wrong, or
> otherwise.  Any help would be appreciated.  I am using version 2.3.0.677.
>
> I did debug into the Ibatis code and noticed that, in BasicResultMap class
> (line 371) that when checking for a unique key, it concatenates all of the
> keys together ("ukey") which I am assuming is anything specified with a
> "groupBy" although I had a little trouble verifying that.  If my
> understanding of the code is correct, this would lead to this situation:
>
> GroupId: %
> Member Id: 1
> Account Ids :  A and B
> Dependent Ids  X and Y
>
> By the time you are populating the last list,  the same dependent would
> get populated twice with the following concatenated keys:
>
> %1AX and %1BX
>
> X represents the same Dependent, but, because the key is not found in the
> map, it is assumed to be a new Dependent and added to the result list.   As
> previously mentioned, if I switch the order of accounts and dependents,  I
> end up with too many accounts, so I do not think it is the groupBy values or
> the mapping.
>
> Am I way off base?  Any thoughts? Any help would be much appreciated.
>
>
> Here is some of the involved SqlMaps:
>
> ----  Group,xml
>
> <resultMap id="group" class="domain.member.Group" groupBy="groupId">
>     <result property="groupId"               column="gr_key"/>
>     ...
> </resultMap>
>
> <resultMap id="groupWithMembersWithDependentsAndAccounts" class="
> domain.member.Group" extends="group">
>         <result property="members"   resultMap="
> Member.memberWithAccountsAndDependents"/>
> </resultMap>
>
> <select id = "getGroupsWithMembersWithAccountsWithDependents"
>     resultMap="groupWithMembersWithDependentsAndAccounts">
>
>     SELECT
>     <include refid="SqlFragment.memberFieldFragment"/>,
>     <include refid="SqlFragment.groupFieldFragment"/>,
>     <include refid="SqlFragment.dependentFieldFragment"/>,
>     <include refid="SqlFragment.accountFieldFragment"/>
>     FROM group, member, depend, account
>     WHERE
>         gr_key == mb_gr_key
>         AND mb_key == dp_mb_key
>         AND mb_key == ms_mb_key
> </select>
>
> ----  Member.xml
>
> <resultMap id="member" class="domain.member.Member" groupBy="id">
>     <result property="id"                         column="mb_key"/>
>     ...
> </resultMap>
>
> <resultMap id="memberWithAccountsAndDependents" class="
> domain.member.Member"
>     extends="member">
>         <result property="dependents" resultMap="Dependent.dependent"/>
>         <result property="accounts"   resultMap="Account.account"/>
> </resultMap>
>
> ----  Account.xml
>
> <!-- Result Maps -->
> <resultMap id="account" class="domain.member.Account"
> groupBy="accountId">
>     <result property="accountId"                  column="ms_key"/>
>     ...
> </resultMap>
>
> ---- Dependent.xml
>
> <!-- Result Maps -->
> <resultMap id="dependent" class="domain.member.Dependent"
> groupBy="dependentId">
>     <result property="dependentId" column="dp_key"/>
>     ...
> </resultMap>
>
> ------------------------------
> Gear up for Halo(r) 3 and get a $25 Best Buy gift card. It's our way of
> saying thanks for using Windows Live™. Get it now!<http://gethalo3gear.com/?ocid=SeptemberWLHalo3_WLHMTxt_1>
>