2014-12-04 56 views
1
Class User{ 
    private String employeeId; 
    private Country assignedCountry; 
    private Region employeeRegion; 

    //getter & setter 
} 

Class Country{ 
    private String countryCode; 
    private Region countryRegion; 
    //getter & setter methods 
} 

Class Region{ 
    private String regionCode; 

} 

<resultMap type="User" id="userResultMap"> 
    <id column="employee_id" property="employeeId" /> 
    <association property="assignedCountry" resultMap="countryResultMap"/> 
    <association property="employeeRegion" resultMap="regionResultMap"/> 
</resultMap> 
<resultMap type="Country" id="countryResultMap"> 
    <id column="country_cd" property="countryCode" /> 
    <association property="countryRegion" resultMap="regionResultMap"/> 
</resultMap> 
<resultMap type="Region" id="regionResultMap"> 
    <id column="region_cd" property="regionCode" /> 
    <id column="region_nm" property="regionName" /> 
</resultMap> 

僱員被分配到一個國家,也屬於一個地區。 國家屬於某個地區,可能與員工地區相同或不同。獲取多個嵌入對象mybatis

該查詢將獲取指定國家和地區的用戶。

select U.*, C.*, R.* from 
User U left outer join Country C 
    on U.assigned_country_cd = C.country_cd 
    left outer join Region R 
    on U.employee_region_cd = R.region_cd 

當我通過mybatis執行查詢並檢查用戶對象時,我可以看到用戶區域設置正確。 但是我也可以看到用戶國家內的Region對象也被設置爲用戶所在的區域。這不應該是這種情況。 (據我所知,我不是在這裏取得國家地區,但如果是這樣,這個對象不應該被設置,而不是設置員工地區的國家地區)

有人可以幫助我如何在國家對象內映射國家的地區?

我是mybatis和ORM的新手。任何幫助闡明這一點,將不勝感激。

回答

3

在查詢中,要加入的Region表,而不是與Country表,但表User,最終返回僱員(和Country對象的countryRegion財產以及)的區域。

在你ResultMapregionResultMap圖如下:

region_cd -> regionCode 
region_nm -> regionName 

你映射region_cd列,同時向user.assignedCountry.countryRegion.regionCode,並在同一時間基本上設置在同一列同一性質的user.employeeRegion.regionCode不同的對象。

你可以做的是區分用戶的地區和國家的區域在您的SQL和地圖在MyBatis的相應:

添加另一個加入到該國連接區域:

select U.*, C.*, R.*, CR.region_cd AS C_region_cd, CR.region_nm as C_region_nm from 
User U left outer join Country C 
    on U.assigned_country_cd = C.country_cd 
    left outer join Region R 
    on U.employee_region_cd = R.region_cd 
    left outer join Region CR 
    on CR.belongsTo = C.country_cd 

而且,您需要更改您的ResultMap,如下所示,以便使用具有不同列名稱的相同ResultMap。在這裏,belongsTo是在你的Region表中顯示它所屬Country列:

<resultMap type="User" id="userResultMap"> 
    <id column="employee_id" property="employeeId" /> 
    <association property="assignedCountry" resultMap="countryResultMap"/> 
    <association property="employeeRegion" resultMap="regionResultMap"/> 
</resultMap> 

<resultMap type="Country" id="countryResultMap"> 
    <id column="country_cd" property="countryCode" /> 
    <association columnPrefix="C_" property="countryRegion" resultMap="regionResultMap"/> 
</resultMap> 

<resultMap type="Region" id="regionResultMap"> 
    <id column="region_cd" property="regionCode" /> 
    <id column="region_nm" property="regionName" /> 
</resultMap> 
+0

感謝您抽出時間詳細解釋。我正在瀏覽mybatis文檔,只是通過columnPrefix屬性。我希望這能解決我面臨的問題。再次感謝。 – nprak 2014-12-04 22:47:51

+0

不客氣,希望它的作品:) – yalpertem 2014-12-04 22:50:28

+0

它確實......謝謝 – nprak 2014-12-05 00:09:02