database query will not populate my mapper <collection> with more than 1 item

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

database query will not populate my mapper <collection> with more than 1 item

leka
Hi,

I have a problem when using collection in my mapper. I have a database
with a Municipality and a School table, a municipality can have
several schools. I want to read out a municipality and its connected
schools from the database.

The municipality java object has a List of Schools (schools) which I
wish to populate.

I have followed the example of collection in the MyBatis-user-
guide.pdf and it seems to work but I always only get 1 (one) school
when reading the list object in municipality even though it should
contain several schools. Following is my mapper and my sql, would that
be enough for someone to help me?


I am injecting mappers, so no MyBatis API used. I somehow seems that
reading out schools only happen once... (i.e only one school is
populated to my list in municipality)


MAPPERFILE.XML
<resultMap id="MunicipalityMap"
                type="se.karl.school.model.domain.Municipality">
                <id property="code" column="MUNICIPALITY_CODE" />
                <result property="name" column="MUNICIPALITY" />
                <collection property="schools"
                        ofType="se.karl.school.model.domain.School"
javaType="java.util.List">
                        <id property="municipalityCode" column="MUNICIPALITY_CODE" />
                        <result property="id" column="UPSEC_SCHOOL_ID" />
                        <result property="code" column="UPSEC_SCHOOL_CODE" />
                        <result property="name" column="UPSEC_SCHOOL" />
                </collection>
        </resultMap>

        <select id="selectByName" resultMap="MunicipalityMap"
                parameterType="string">
                select m.MUNICIPALITY, m.MUNICIPALITY_CODE,
                m.COUNTY_CODE, us.UPSEC_SCHOOL, us.UPSEC_SCHOOL_CODE from
                beda.MUNICIPALITIES m
                left outer join beda.UPSEC_SCHOOLS us on
                us.MUNICIPALITY_CODE = m.MUNICIPALITY_CODE
                where m.MUNICIPALITY like
                #{name}
                ORDER BY us.UPSEC_SCHOOL ASC
    </select>

CONTEXT.XML
        <bean id="municipalityDao"
class="org.mybatis.spring.MapperFactoryBean">
                <property name="sqlSessionFactory" ref="sqlSessionFactory" />
                <property name="mapperInterface"
value="se.karl.school.dao.IMunicipalityDao" />
        </bean>




thanks in advance / Karl Leidner
Reply | Threaded
Open this post in threaded view
|

Re: database query will not populate my mapper <collection> with more than 1 item

leka
Hi, I solved the issue by removing the line:
<id property="municipalityCode" column="MUNICIPALITY_CODE" />
from the <collection>

anyone got a clue on why that should not have been there, also in the
pdf-documentation on mybatis there is a <id property> tag in the
collection (page 39).

/ Karl

On 15 Okt, 15:33, leka <[hidden email]> wrote:

> Hi,
>
> I have a problem when using collection in my mapper. I have a database
> with a Municipality and a School table, a municipality can have
> several schools. I want to read out a municipality and its connected
> schools from the database.
>
> The municipality java object has a List of Schools (schools) which I
> wish to populate.
>
> I have followed the example of collection in the MyBatis-user-
> guide.pdf and it seems to work but I always only get 1 (one) school
> when reading the list object in municipality even though it should
> contain several schools. Following is my mapper and my sql, would that
> be enough for someone to help me?
>
> I am injecting mappers, so no MyBatis API used. I somehow seems that
> reading out schools only happen once... (i.e only one school is
> populated to my list in municipality)
>
> MAPPERFILE.XML
> <resultMap id="MunicipalityMap"
>                 type="se.karl.school.model.domain.Municipality">
>                 <id property="code" column="MUNICIPALITY_CODE" />
>                 <result property="name" column="MUNICIPALITY" />
>                 <collection property="schools"
>                         ofType="se.karl.school.model.domain.School"
> javaType="java.util.List">
>                         <id property="municipalityCode" column="MUNICIPALITY_CODE" />
>                         <result property="id" column="UPSEC_SCHOOL_ID" />
>                         <result property="code" column="UPSEC_SCHOOL_CODE" />
>                         <result property="name" column="UPSEC_SCHOOL" />
>                 </collection>
>         </resultMap>
>
>         <select id="selectByName" resultMap="MunicipalityMap"
>                 parameterType="string">
>                 select m.MUNICIPALITY, m.MUNICIPALITY_CODE,
>                 m.COUNTY_CODE, us.UPSEC_SCHOOL, us.UPSEC_SCHOOL_CODE from
>                 beda.MUNICIPALITIES m
>                 left outer join beda.UPSEC_SCHOOLS us on
>                 us.MUNICIPALITY_CODE = m.MUNICIPALITY_CODE
>                 where m.MUNICIPALITY like
>                 #{name}
>                 ORDER BY us.UPSEC_SCHOOL ASC
>     </select>
>
> CONTEXT.XML
>         <bean id="municipalityDao"
> class="org.mybatis.spring.MapperFactoryBean">
>                 <property name="sqlSessionFactory" ref="sqlSessionFactory" />
>                 <property name="mapperInterface"
> value="se.karl.school.dao.IMunicipalityDao" />
>         </bean>
>
> thanks in advance / Karl Leidner