これは、MyBatisのXML構成の一部です。
<sql id="selectElementWithAttributes">
SELECT
e.id as id,
e.title as title,
e.type as type,
e.date as date,
e.ownerid as ownerid,
a.attributeid as attributeid,
a.value as value,
a.type as a_type
FROM
test_element as a
LEFT JOIN
test_elementattributes as a
ON
e.id
= a.parentid
</sql>
ここで、resultMapを使用して列をオブジェクトにマップします。
<resultMap type="Element" id="rmElement">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="type" column="type" />
<result property="date" column="date" />
<result property="ownerid" column="ownerid" />
<collection property="attributes" ofType="Attribute">
<id property="{id=parentid,attributeid=attributeid}" />
<result property="key" column="attributeid" />
<result property="value" column="value" />
</collection>
</resultMap>
問題は、Elementオブジェクトにコレクションを作成する必要があることです。両方のコレクションに属性が含まれています。違いは、属性のタイプ(a_typeにマップされる)です。このタイプに応じて、コレクション1またはコレクション2に属性を含めるのが好きです。ディスクリミネーターをいじってみましたが、何をしていたのかよくわからず、機能しません...
これはアイデアですが、a_typeに応じてコレクションを切り替えるにはどうすればよいですか?
<resultMap type="Element" id="rmElement">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="type" column="type" />
<result property="date" column="date" />
<result property="ownerid" column="ownerid" />
<collection property="attributes" ofType="Attribute">
<id property="{id=parentid,attributeid=attributeid}" />
<result property="key" column="attributeid" />
<result property="value" column="value" />
</collection>
<collection property="attributesOther" ofType="Attribute">
<id property="{id=parentid,attributeid=attributeid}" />
<result property="key" column="attributeid" />
<result property="value" column="value" />
</collection>
</resultMap>
前もって感謝します