简体   繁体   中英

Mybatis “java.lang.IllegalArgumentException: Result Maps collection already contains value for” error when mapping nested classes

I'm getting a java.lang.IllegalArgumentException: Result Maps collection already contains value for" error when trying to do some XML mappings on a nested class:

I have the following nested classes structure:

package com.test;   
public class ObjectA {
  private List<ObjectB> entries;

  public ObjectA(List<ObjectB>  entries) {
    this.entries = entries;
  }

  public void setEntries(List<ObjectB> entries) {
    this.entries = entries;
  }

  public List<ObjectB> getEntries() {
    return this.entries;
  }

  public class ObjectB {
    private String param1;

    public ObjectB(String param1) {
        this.param1 = param1;
    }

    public void setParam1(String param1) {
        this.param1 = param1;
    }

    public String getParam1() {
        return this.param1;
    }

  }
}

And the xml mappers look like this:

<resultMap id="ObjectA" type="com.test.ObjectA">
    <collection property="entries" resultMap="Entries"/>
</resultMap>
<resultMap id="Entries" type="com.test.ObjectA.ObjectB">
    <result property="param1" column="column1"/>
</resultMap>

If i take the ObjectB class outside of ObjectA class, the mapping works fine. But the thing is that i don't want to do that. I want to have it nested.

Thanks

I see two problems here:

  1. The separator for the name of an inner class to its surrounding class is $ and not . . So instead of com.test.ObjectA.ObjectB your path should be com.test.ObjectA$ObjectB .
  2. You ObjectB has not default constructor and you don't specify a constructor mapping. In addition to your explicit argument, remember that an inner class always has its outer class as implicit constructor argument unless you declare that class as static ( public static class ObjectB... ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM