MyBatis collection in association returns only one row

I have to use MyBatis for a project and I am trying to define a resultMap with a collection in my association.

I'll simplify my problem, so we'll assume my database has two tables:

World [idWorld, worldName]

Level [IdLevel, levelName, idWorld]

I want to connect every world and every level. At the moment, I manage to get the result using this scorecard:

<resultMap type="World" id="WorldResultMap">
    <id property="idWorld" column="idWorld"/>
    <result property = "worldName" column="worldName"/>
        <collection property="level" javaType="java.util.ArrayList" ofType="Level">
            <result property="idLevel" column="idLevel" />
            <result property="levelName" column="levelName" />
        </collection>
</resultMap>

      

But after displaying, I get this result:

  <worlds>
        <world>
            <idWorld>1</idWorld>
            <worldName> Wordl name</worldName>
            <level> ... </level>
            <level> ... </level>
        </world>
         <world>
            <idWorld>2</idWorld>
            <worldName> Wordl name</worldName>
            <level> ... </level>
            <level> ... </level>
            <level> ... </level>
        </world>
<worlds>

      

When I want something like this:

<worlds>
            <world>
                <idWorld>1</idWorld>
                <worldName> Wordl name</worldName>
                <levels>
                        <level> ... </level>
                        <level> ... </level>
                 </levels> 
            </world>
             <world>
                <idWorld>2</idWorld>
                <worldName> Wordl name</worldName>
                <levels>
                          <level> ... </level>
                          <level> ... </level>
                          <level> ... </level>
                </levels>
            </world>
    <worlds>

      

I am trying to get this result with the following result map:

<resultMap type="World" id="WorldResultMap">
        <id property="idWorld" column="idWorld"/>
        <result property = "worldName" column="worldName"/>
            <association property="levels" javaType="Levels">
                 <collection property="level" javaType="java.util.ArrayList" ofType="Level">
                     <result property="idLevel" column="idLevel" />
                     <result property="levelName" column="levelName" />
                 </collection>
            </association>
    </resultMap>

      

I change my essence to have a list of levels in levels and not more in the world. My problem is that I am only getting one level for each world in my last scorecard with the same query. I think I missed understanding the use of "association". How can I get the entire level in levels?

Any help would be appreciated, thanks in advance.

+3


source to share


1 answer


replace

<result property="idLevel" column="idLevel" />

      

to

<id property="idLevel" column="idLevel" />

      

then you should get the expected result.




Quoting from mybatis-doC # Result_Maps :

Very Important: The id elements play a very important role in the rendering of the Nested Result. You should always specify one or more properties that can be used to uniquely identify the results. The truth is, MyBatis will work if you don't leave it, but at high performance. Choose as many properties as possible that can uniquely identify the result. The primary key is the obvious choice (even if it is composite).

If id

not present, "MyBatis will work". It seems that MyBatis will filter out the duplicate lines it counts.

+4


source







All Articles