[ORM][MyBatis] 単純な文字列リストを入れ子に持つBeanへのResultMap定義

■ 事象
The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)". mybatis
要素タイプ"resultMap"のコンテンツは"(constructor?,id*,result*,association*,collection*,discriminator?)"と一致する必要があります。
org.apache.ibatis.builder.BuilderException
org.xml.sax.SAXParseException
 
■ 原因
resultMap の Schema 違反
 
■ 対応
以下のように result の定義を全部 collection の前にする
|    <'resultMap id="accountMap" type="myproject.Account">
|        <'id property="userId" column="user_id" />
|        <'result property="firstName" column="first_name" />
|        <'result property="lastName" column="last_name" />
|        <'result property="status" column="status" />
|        <'result property="lastUpdate" column="last_update" />
|               <'collection property="roles" ofType="java.lang.String" javaType="list">
|                   <'result column="role_column" />
|               <'/collection>
|    <'/resultMap>
 
Beanのfield定義順にしたいところだけど、以下のようにしてはダメ
|    <'resultMap id="accountMap" type="myproject.Account">
|        <'id property="userId" column="user_id" />
|        <'result property="firstName" column="first_name" />
|        <'result property="lastName" column="last_name" />
|               <'collection property="roles" ofType="java.lang.String" javaType="list">
|                   <'result column="role_column" />
|               <'/collection>
|        <'result property="status" column="status" />
|        <'result property="lastUpdate" column="last_update" />
|    <'/resultMap>
 
[参考] SQL 定義はこんな感じ
    <'select id="findOne" parameterType="String" resultMap="accountMap">
    <'![CDATA[
        SELECT
            a.user_id,
            a.first_name,
            a.last_name,
            r.role as role_column,
            a.status,
            a.last_update
        FROM
            ACCOUNT_TBL a, ROLE_TBL r
        WHERE
            a.user_id = #{userId}
            AND a.user_id = r.rltbl_user_id
    ]]>
    <'/select>
 
[参考] Beanはこんな感じ
public class Account implements Serializable {
 
              private static final long serialVersionUID = 1L;
 
              private String userId;//user_id
              private String firstName;//first_name
              private String lastName;//last_name
              private List<'String> roles;//role
              private String status;//status
              private Date lastUpdate;
 
[参考] こんな書き方もいろいろ ― 未検証w
 
    <'select id="findOne" parameterType="String" resultMap="accountMap">
    <'![CDATA[
        SELECT
            a.user_id,
            a.first_name,
            a.last_name,
            (
                SELECT r.role as role_column
                FROM role_tbl r
                WHERE rltbl_user_id = #{userId}
            ),
            a.status,
            a.last_update
        FROM
            ACCOUNT_TBL a, ROLE_TBL r
        WHERE
            a.user_id = #{userId}
            AND a.user_id = r.rltbl_user_id
    ]]>
    <'/select>
 
----------
             
    <'resultMap id="accountMap" type="account.master.system.pilot.domain.model.Account">
        <'id property="userId" column="user_id" />
        <'result property="firstName" column="first_name" />
        <'result property="lastName" column="last_name" />
        <'result property="status" column="status" />
        <'result property="lastUpdate" column="last_update" />
        <'result property="roles" javaType="java.util.List" resultMap="mapForPropRoles" />
    <'/resultMap>
    <'resultMap id="mapForPropRoles" type="java.lang.String">
        <'result property="roles" column="role_column" jdbcType="VARCHAR" javaType="java.lang.String" />
    <'/resultMap>
             
----------
 
    <'resultMap id="accountMap" type="account.master.system.pilot.domain.model.Account">
        <'id property="userId" column="user_id" />
        <'result property="firstName" column="first_name" />
        <'result property="lastName" column="last_name" />
        <'result property="status" column="status" />
        <'result property="lastUpdate" column="last_update" />
        <'collection property="roles" column="userId" javaType="ArrayList" ofType="String" select="findUserRoles">
            <'result property="value" column="role_column"/>
        <'/collection>
    <'/resultMap>
 
                  <'select id="findUserRoles" resultType="String">
                  <'![CDATA[
                      SELECT
                          role as role_column
                      FROM
                          ROLE_TBL
                      WHERE
                         rl_user_id = #{userId}
                  ]]>
    <'/select>
 
----------
 
ref.
http://mybatis.org//dtd/mybatis-3-mapper.dtd
Mybatis resultmap to map values to hash map field of a model, stckoverflow, Dec 6 '12 at 16:46
http://stackoverflow.com/questions/13548349/mybatis-resultmap-to-map-values-to-hash-map-field-of-a-model
https://github.com/mybatis/mybatis-3/issues/364
http://stackoverflow.com/questions/1127992/how-do-you-map-a-liststring-in-ibatis
ネストされた select を使って collection を読み込む
http://www.mybatis.org/mybatis-3/ja/sqlmap-xml.html
Nested Select for Collection
http://www.mybatis.org/mybatis-3/sqlmap-xml.html
 
tag:
ORM MyBatis iBatis nested list string resultmap collecon BuilderException SAXParseException 1:N

tag : ORM MyBatis iBatis nested list string resultmap collecon BuilderException SAXParseException

2017-03-29 23:04 : 開発 : コメント : 0 : トラックバック : 0 :
コメントの投稿
非公開コメント

« next  ホーム  prev »

search

ad



counter


tag cloud

category cloud