簡體   English   中英

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

在 db 層,路由具有 TmpCase 的外鍵:route.pid -> tmpCase.sid。

  1. 已經嘗試過:在沒有包裝 MigrationData 的情況下使其相同並且它按預期工作,但我嚴格需要這種結構並使用 columnPrefix。
  2. 問題:我得到錯誤的映射,即 insted 獲取 TmpCase 列表,其中包含路由列表,我得到的 TmpCase 列表只有一個路由元素。
  3. 預期:TmpCase.getRoutes() 是多個元素的列表
  4. 實際: TmpCase.getRoutes() 是一個或零個元素的列表。

我認為這可能是我誤解了關聯塊內的工作收集塊與 columnPrefix 的關系。 我正在閱讀文檔,但沒有。 我會很高興得到任何幫助。

我找到了解決方案。 層次結構中的每個 class 必須具有正確工作的收集塊。 在我的情況下,頂級 class PreMigrationData 在數據庫中沒有 id。 我讓他們從子類中識別出來,這對我來說很好用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM