ResultMap分けてなくてNestedSQLException
ちゃんと基本わかってればはまらないことだけど。
エラー内容がわかりにくくてまたはまる可能性あるので。
一見大丈夫そうなsqlMapなのにNestedSQLExceptionが出る件。
com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in sqlMap/xml_name.xml. --- The error occurred while applying a result map. --- Check the namespace.resultMapId. --- Check the result mapping for the 'propertyName' property. --- Cause: com.ibatis.sqlmap.client.SqlMapException: Error getting nested result map values for 'basePictures'. Cause: java. sql.SQLException: Column 'column_name' not found. … Caused by: com.ibatis.sqlmap.client.SqlMapException: Error getting nested result map values for 'basePictures'. Cause: java.sql.SQLException: Column 'column_name' not found. … ... 30 more Caused by: java.sql.SQLException: Column 'column_name' not found. …
で、↓がXML。
<resultMap id="pkResult" class="jp.co.aaaa.bbb.data.BaseItem" > <result ・・・ /> </resultMap> <!-- 検索結果 : 拡張Bean --> <resultMap id="extResult" class="jp.co.aaaa.bbb.data.BaseItem" extends="pkResult"> … <result property="namespace2Properties" resultMap="namespace2.extResult" /> <result property="namespace3Properties" resultMap="namespace3.extResult" /> </resultMap> <!-- SQL文 : プライマリ検索 --> <select id="selectByPrimaryKey" parameterClass="Integer" resultMap="extResult"> SELECT <include refid="namespace1.select" /> , <include refid="namespace2.select" /> FROM namespace1 as baseitem LEFT JOIN test.namespace2 as cndt ON baseitem.namespace1_id = cndt.namespace1_id WHERE baseitem.namespace1_id = #Id# </select> <!-- SQL文 : 条件検索 --> <select id="selectByOption" parameterClass="jp.co.aaa.bbb.dao.FindOption" resultMap="extResult"> SELECT <include refid="namespace1.select" /> , <include refid="namespace2.select" /> , <include refid="namespace3.select" /> FROM ( namespace1 as baseitem LEFT JOIN namespace2 as cndt ON baseitem.namespace1_id = cndt.namespace1_id ) LEFT JOIN namespace3 as pict ON baseitem.namespace1_id = pict.namespace1_id <include refid="where" /> <include refid="orderBy" /> <include refid="limit" /> </select> </sqlMap>
PK検索の時はnamespace3いらないんでJOINしてないんですが、プロパティーはextResultのresultmapで紐付けられてる。
ので、NestedSQLException発生。
そらそうだわな。
なので↓のようにResultMapをちゃんと分けて、使いたいメンバーにのみマッピングしてあげる。
<!-- プライマリ検索用ResultMap --> <resultMap id="primaryResult" class="jp.co.aaa.bbb.data.BaseItem" extends="extResult" groupBy="namespace1_id"> <result property="namespace2Properties" resultMap="namespace2.extResult" /> </resultMap> <!-- 条件検索用ResultMap --> <resultMap id="optionResult" class="jp.co.aaa.bbb.data.BaseItem" extends="extResult" groupBy="namespace1_id"> <result property="namespace2Properties" resultMap="namespace2.extResult" /> <result property="namespace3Properties" resultMap="namespace3.extResult" /> </resultMap> <!-- SQL文 : プライマリ検索 --> <select id="selectByPrimaryKey" parameterClass="Integer" resultMap="primaryResult"> … </select> <!-- SQL文 : 条件検索 --> <select id="selectByOption" parameterClass="jp.co.aaa.bbb.dao.FindOption" resultMap="optionResult"> … </select>
30分ほど悩んだ。。。orz