mybatismybatis-mappermybatis-sql

MyBatis mapping collection inside association


I have a next resultMap

        <resultMap id="resultMap" type="***.PreMigrationData"
                   autoMapping="true">
...
     <association property="tmpCase" javaType="***.TmpCase" columnPrefix="i_">
                 <id column="sid" property="sid"/>
            <result column="pid" property="pid"/>
                <collection property="routes" ofType="***.Route"
                            resultMap="routes" columnPrefix="rt_"/>
            </association>
        </resultMap>
    
        <resultMap id="routes" type="***.Route">
            <result column="sid" property="sid"/>
            <result column="pid" property="pid"/>
            ...
        </resultMap>

Entities clases.

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class PreMigrationData{


   ...
    private TmpCase tmpCase;
}



@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class TmpCase {

    private Long sid;

    private Long pid;
    ...

    private List<Route> routes;
}

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Route {
    private Long sid;
    private Long pid;
}

SQL:

    <select id="getCases" resultMap="resultMap">

        <include refid="select"/>
WHERE ...
</select>
        <sql id="select">
            select
            <include refid="case">
                <property name="alias" value="pi"/>
                <property name="prefix_source" value="i_"/>
            </include>,
    <include refid="rotue">
                <property name="alias" value="pir"/>
                <property name="prefix_source" value="i_rt_"/>
            </include>
      from tmp_case pi
    LEFT JOIN route pir on pir.PID = pi.SID

In db layer, route have foreign key to TmpCase: route.pid -> tmpCase.sid.

  1. Have tried: made it same without wraper MigrationData and it work as expected, but i strictly need exactly this structure and uses columnPrefix.
  2. Problem: i get wrong mapping, i.e. insted get list of TmpCase, which consist list of route, i got list of TmpCase with only one element of route.
  3. Expected: TmpCase.getRoutes() is list of multiply elements
  4. Actual: TmpCase.getRoutes() is list of one or zero element.

I think it can be that i misunderstanding how work collection block inside association block with columnPrefix. I was reading documnetation, but nothing. I will be glad for any help.


Solution

  • I finded solution. Each of class in hierarchy must have for correctly work collection block. In my case top level class PreMigrationData haven't id in db. I let them id from subclass and it's work fine for me