0

これは、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>

前もって感謝します

4

1 に答える 1

1

このようなものを使用したことはないので、ただのアイデアです。クエリを変更して2セットの属性を返し、読み取り側のNULLを除外しますか?

<sql id="selectElementWithAttributes">
    SELECT
    e.id as id,
    e.title as title,
    e.type as type,
    e.date as date,
    e.ownerid as ownerid,
    CASE WHEN a.type = 'attribute1' THEN a.attributeid ELSE NULL END as attribute1id,
    CASE WHEN a.type = 'attribute2' THEN a.attributeid ELSE NULL END as attribute2id,
    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>

次に、2つの異なるコレクションをattribute1とattribute2の結果マップに配置します。両方のコレクションが完全なセットを取得しますが、セットに属していない属性の値はNULLになります。または、NULLが正当な値である場合は、別のフラグを使用してください。コレクションへのNULLの挿入をスキップする方法があればさらに良いでしょう。

ばかげた提案かもしれませんが、誰が知っていますか?おそらく別のアプローチへのヒント?[ああ、そして明白なこと以外に:2つのクエリを作成する]

于 2012-05-23T20:52:59.780 に答える