Composite keys in MyBatis <collection> mappings

痞子三分冷 提交于 2019-12-13 17:08:17

问题


I am unable to pass a composite key to a MyBatis <collection> element (using version 3.2.7). The MyBatis documentation states:

Note: To deal with composite keys, you can specify multiple column names to pass to the nested select statement by using the syntax column="{prop1=col1,prop2=col2}". This will cause prop1 and prop2 to be set against the parameter object for the target nested select statement.

However, all my attempts to implement this produce the Exception

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class java.lang.Integer with invalid types () or values (). Cause: java.lang.NoSuchMethodException: java.lang.Integer.<init>()

The collection (which resides in another ResultsMap) is:

<collection property="foos" ofType="FooObject"
    column="{param1=user_id,param2=foo_id}" select="getFoosByUser" >
        <id property="userId" column="user_id" />
        <id property="foo" column="foo_id" />
        <result property="fooName" column="foo_name" />
</collection>

It should return an ArrayList of Foo objects. The composite key is user_id and foo_id. The select query is:

    <select id="getFoosByUser" parameterType="Integer" resultType="FooObject">
        SELECT
          user_id AS userId,
          foo_id AS fooId, 
          foo_name AS fooName
        FROM foo_table
        WHERE user_id = #{param1}
        AND foo_id = #{param2}
    </select>

The query works correctly if I only use one parameter, e.g. removed foo_id=#{param2} and then use column=user_id in the collection, but I cannot work out how to structure the column attribute correctly for two keys. Any ideas?


回答1:


MyBatis is confused by using parameterType when there are more than one parameter. Modify you query mapping like this:

<select id="getFoosByUser" resultType="FooObject">
    SELECT
      user_id AS userId,
      foo_id AS fooId, 
      foo_name AS fooName
    FROM foo_table
    WHERE user_id = #{param1}
    AND foo_id = #{param2}
</select>


来源:https://stackoverflow.com/questions/27020095/composite-keys-in-mybatis-collection-mappings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!