0

Getting this error while iterating over Map in mybatis

Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='__frch_key_0', mode=IN, javaType=class java.util.UUID, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.

Mapper method

int updateUser(@Param("params") Map<UUID, UUID> params);

query

<update id="updateUser">
        UPDATE users AS u
        SET role_id = CAST(p.roleid AS uuid)
        FROM (VALUES
        <foreach collection="params" item="item" index="index"  open="('" separator="'),('" close="')">
            #{index} ',' #{item} 
        </foreach>
        )
        AS p(user_id ,roleid)
        WHERE CAST(p.user_id AS uuid) = u.user_id
    </update>
3
  • #{} is a placeholder in a java.sql.PreparedStatement, so it should not be enclosed with single quotes. Commented May 25, 2022 at 14:21
  • Thanks for the comment. I have replaced #{} with ${} and the issue is resolved. Commented May 25, 2022 at 14:57
  • It may work, but it's not the recommended approach. Please see the FAQ entry. Commented May 25, 2022 at 15:02

1 Answer 1

1

I solved the issue by replacing the #{index} #{item} to ${index} ${item}. May be foreach works different map type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.