ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Fábio Pisaruk" <pisa...@gmail.com>
Subject GroupBy column insted of property doesn´t work
Date Fri, 14 Sep 2007 14:29:03 GMT
Hi,

Is there a way to use groupBy with a column name?
I need to do so 'cause there is no property that uniquely identify the bean
and i am not able to change it do add one.
For example:
Suppose i´ve got two tables: Person and Address.

Person:
    person_id
    first_name
    last_name

Address:
    person_id
    address_id
    address
    country
    city

And two beans:
Person
    firstName
    lastName
    addresses(Address[])
Address
    address
    country
    city

My maps:

<resultMap id="resulMap-person" class="Person" groupBy="person_id">
    <result property="firstName" column="first_name"/>
    <result property="lasttName" column="last_name"/>
    <result property="addresses" resultMap="resultMap-adress"/>
</resultMap>

<resultMap id="resulMap-address" class="Address">
    <result property="address" column="address"/>
    <result property="country" column="country"/>
    <result property="city" column="city"/>
</resultMap>

My sql:

<select id="get-person-by-id" parameterClass="int"
resultMap="resulMap-person">
    select p.person_id,p.first_name,p.last_name,a.address,a.city,a.country
    from Person p ,Address a
    where p.person_id=#value# and p.person_id=a.person_id
</select>

In doing so i am not getting the desired result.
Person information are replicated for each address it contains.

PS: I know two workarounds that i don´t consider good solutions:
1-) Creating a wrapperPerson with a person_id attribute and having Ibatis
grouping result on it or
2-) using a nested select to get address for each person:
     <result property="addresses" select="get-addresses-by-person_id"
column="person_id"/>
<select id="get-addresses-by-person_id" parameterClass="int"
resultMap="resultMap-adress">
    select * from Address where person_id=#value#
</select>

Thanks in advance

-- 
Visto como se não executa logo a sentença sobre a má obra, o coração dos
filhos dos homens está inteiramente disposto a praticar o mal.


--Nerd´s sign

If you have four classes, Everybody, Somebody, Anybody, and Nobody, if
Somebody has a bug, it could be Anybody 's fault but Nobody really knows,
while Everybody shares responsibility.

"Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the universe trying to build
bigger and better idiots. So far, the universe is winning." - Rick Cook
Mime
View raw message