ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Carlos Cajina - Hotmail 1" <cecaj...@hotmail.com>
Subject Re: [Table field names VS. Object properties names]
Date Thu, 25 Jan 2007 21:25:46 GMT
Hi again!

Thanks for the quick comment Larry.

You're absolutely right, I does make sense at any level (Java/SQL)... I 
guess my brain took a short vacation... :^)

Sorry for asking the obvious.

Regards,

    Carlos


----- Original Message ----- 
From: "Larry Meadors" <lmeadors@apache.org>
To: <user-java@ibatis.apache.org>
Sent: Wednesday, January 24, 2007 6:23 AM
Subject: Re: [Table field names VS. Object properties names]


> That is correct, and makes perfect sense if you think about it at the SQL 
> level.
>
> If you run that SQL statement in a SQL tool, and do not alias that
> field, how would you look at the results and know which was which?
>
> Larry
>
>
> On 1/23/07, Carlos Cajina <cecajina@hotmail.com> wrote:
>> 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
>>
>>
> 


Mime
View raw message