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.
Advertisement
Answer
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>