Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: Result Maps collection already contains value for

Hey I'm using Mybatis with Spring Annotations.

and getting this error:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mypackage.mappers.QuestionsMapper.Question

here is the domain class (sans getters and setters):

public class Question {


    String optionsAsString;
    String typeAsString;
    Integer fieldId;
    String title;
    String description;

    public Question(){
    }   
}

here is my Mapper.Java class

@MapperScan
public interface Mapper {

public List<Question> getQuestions(@Param("shifts") List<Integer> shifts, @Param("job_id") Integer job_id);
}

lastly here is the Mapper.xml

<mapper namespace="com.mypackage.mappers.Mapper">
<resultMap type="com.mypackage.domain.Question" id="Question">
    <id column="field_id" property="fieldId" />
    <result column="data_type" property="typeAsString" />
    <result column="title" property="title" />
    <result column="description" property="description" />
    <result column="options" property="optionsAsString" />
</resultMap>

<select id="com.mypackage.mappers.Mapper.getQuestions" resultMap="Question" timeout="10">
    SELECT
    f.field_id,
    f.data_type,
    f.title,
    f.options,
    f.description 
    FROM
        (SELECT DISTINCT q.*
        FROM
            question_services qs INNER JOIN
            questions q 
            ON qs.field_id=q.field_id AND q.job_id = qs.job_id INNER JOIN
            services s
            ON qs.service_id = s.service_id and qs.job_id = s.job_id
            WHERE s.job_id = #{job_id} AND s.service_id in
            <foreach item="shift" collection="shifts" open="(" separator="," close=")">
                #{shift}
            </foreach>
        ) f
</select>

I'm inclined to believe there is something wrong with the xml select statement. Probably with how I am using foreach. I have another mapper using a similar format it just does not use for each and it is not having any problems.

like image 526
Russell Fillmore Avatar asked Jan 06 '23 01:01

Russell Fillmore


2 Answers

Adding this answer since in my case the problem was different but the exception was same . The exception said ,

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mypackage.model.mymapper.BaseResultMap

This can also happen if you have the same mapper xml present in multiple locations and that duplicate xml gets picked up and parsed. Its good to check that even if by mistake you have taken a backup of the mapper xmls and that lies in the location or sub folders which the mybatis configurer scans for.

like image 73
Manu Bhat Avatar answered Jan 08 '23 15:01

Manu Bhat


Yep It looks like there was an error somewhere in my select statement. I ended up just rewriting it a different way.

<select id="getQuestions" resultMap="Question">
    SELECT
        q.field_id,
        q.data_type,
        q.title,
        q.description,
        q.options
    FROM
        questions q
    WHERE
        job_id = #{job_id}
    AND
        field_id
    IN
        (SELECT 
            fs.field_id
        FROM
            question_services qs
        INNER JOIN 
            services s 
        ON
            qs.service_id = s.service_id 
        AND
            qs.job_id = s.job_id
        WHERE
            s.job_id=#{job_id}
        AND
            s.service_id
        IN
        <foreach item="item" index="index" collection="shifts" open="(" separator="," close=")">
            #{item}
        </foreach>
        );
</select>
like image 29
Russell Fillmore Avatar answered Jan 08 '23 15:01

Russell Fillmore