Hi!

I have a question regarding the implementation of the N+1 solution. Couldn't find any reference in the mailing list/docs, so here's the thing:

Preconditions:
1. Using iBATIS version 2.3.0.677
2. Applied the N+1 solution to a One-to-Many tables relationship

Problem details:
1. table_b references table_a with a FK
2. table_a and table_b both have a 'name' field
3. Java classes representing those tables also have a 'name' property

Problem:
Since both tables have a 'name' field, when doing a queryForList and traversing the resulting list of A objects that contain a list of B objects, and calling getName() for both the parent and children objects, the returned value is always the 'name' property value of the children objects.

I aliased the 'name' field of table_b in my query (and in the appropiate SqlMap) and the traversing and calling of getName() in parents and children worked as expected.

I'm guessing -from the behaviour I see- that when mapping tables that share field names (i.e. id, name, description) the query in the SqlMap must alias those fields so that the SqlMaps/Java objects don't "get confused". Sorry I can't describe this in a more technical way but Is this behaviour correct?

Below is a mapping that mimics what I'm doing/using:

<resultMap class="some.package.ClassA" id="resultA" groupBy="aId">
       <result column="a_id" jdbcType="INTEGER" property="aId" />
       <result column="name" jdbcType="VARCHAR" property="name" />
       <!-- 1:N solution -->
       <result property="bList" resultMap="someNameSpace.resultB" />
   </resultMap>

   <resultMap class="some.package.ClassB" id="resultB">
       <result column="b_id" jdbcType="INTEGER" property="bId" />
       <result column="b_name" jdbcType="VARCHAR" property="name" />
<result column="componentType" jdbcType="INTEGER" property="component_type" />
       <result column="a_id" jdbcType="INTEGER" property="aId" />
   </resultMap>

   <select id="selectAB" parameterClass="int" resultMap="resultA">
       SELECT a.a_id, a.name, b.b_id, b.nname, b.component_type, b.a_id
       FROM table_a a INNER JOIN table_b b
       ON (a.a_id = b.a_id)
       WHERE b.component_type = #value#
       ORDER BY a.name, b.name
   </select>

Any comments, thoughts, ideas will be appreciated.

Regards,

Carlos

Reply via email to