0
votes

I use myBatis. Single record statement works ok, but when I try to use foreach to perform update of list of records an error occurs regarding mapping a bit weird since it works ok for single record.

@Update("UPDATE table SET field_one=#{input.fieldOne} WHERE field_two =#{input. fieldTwo}")
    public void updateDomain(@Param("input") ObjectList input);

@Update({"<script>",
        "<foreach item='item' index='index' collection='input' separator=','>",
        "UPDATE table",
        "SET field_one = '#{item.fieldOne}'",
        "WHERE field_two = '#{item.fieldTwo}'", 
        "</foreach> ",
        "</script>"})
public void updateDomains(@Param("input") List<ObjectList> input);

Here's the error I get:

Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='__frch_item_0.fieldOne', mode=IN, javaType=class java.lang.String, 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: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:89) at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:93) Caused by: 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: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:55) at org.apache.ibatis.scripting.defaults.DefaultParameterHandler.setParameters(DefaultParameterHandler.java:87) ... 45 common frames omitted Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

1

1 Answers

0
votes

Actually it was syntax tyopo should ; instead of , for separator. Here is Workable Example.

@Update({ "<script>",
            "<foreach item='item' index='index' collection='collectionToUpdate' separator=';'>",
            "UPDATE domains",
            "SET columnOne =  #{item.valueOne} WHERE columnTwo = #{item.valueTwo}", 
            "</foreach> ",
            "</script>"})