关联内部的 MyBatis 映射集合

MyBatis mapping collection inside association

我有下一个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>

实体类。

@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

在数据库层,路由有外键到 TmpCase:route.pid -> tmpCase.sid.

  1. 已尝试:在没有包装器 MigrationData 的情况下使它相同并且它按预期工作,但我严格需要这个结构并使用 columnPrefix。
  2. 问题:我得到了错误的映射,即我得到了包含路由列表的 TmpCase 列表,我得到了只有一个路由元素的 TmpCase 列表。
  3. 预期:TmpCase.getRoutes() 是相乘元素的列表
  4. 实际:TmpCase.getRoutes() 是一个或零元素的列表。

我认为可能是我误解了关联块中的集合块如何使用 columnPrefix。我正在阅读文档,但什么也没有。我很乐意提供任何帮助。

我找到了解决方案。层次结构中的每个 class 都必须具有正确工作的集合块。在我的例子中,顶级 class PreMigrationData 在数据库中没有 id。我让他们使用 subclass 的 id,这对我来说很好