7

I encountered problems when returning a list of Objects inside another Object when using MyBatis. My main object looks like this:

private Long id;

private String symbol;

private List<TypePermission> typePermissions;

and my mapper looks like this

<resultMap type="CalendarType" id="calendarTypeMap">
    <result column="id" property="id"/>
    <result column="symbol" property="symbol"/>
    <collection property="TypePermissions" resultMap="TypePermissions"/>
</resultMap>

<resultMap id="TypePermissions" type="TypePermission">
    <result property="roleId" column="roleId"/>
    <result property="permissionSymbol" column="permissionSymbol"/>
</resultMap>

My goal is to get an object like this:

content:[
    "id":id,
    "symbol":symbol,
    "TypePermissions":{
        "roleId":roleId,
        "permissionSymbol":permissionSymbol
    }
]

When I execute the sql query I get the following an error cannot find symbol TypePermissions, because the main SELECT tries to select rows such as TYPEPERMISSIONS, ID, SYMBOL

I searched over the internet, but failed to find anything useful. Could you help me and point out what am I doing wrong?

1 Answer 1

9

Please post your select snippet, I think this will ok:

<select id="selectCalendarType" parameterType="int" resultMap="calendarTypeMap">
    SELECT c.id,
    c.symbol
    t.roleId,
    t.permissionSymbol
    FROM CalendarType c
    LEFT JOIN TypePermission t ON c.id = t.c_id
    WHERE c.id = #{id}
</select>

And I think what you will get is actully something like this:

content:{
  "id":id,
  "symbol":symbol,
  "TypePermissions":[{
    "roleId":roleId,
    "permissionSymbol":permissionSymbol
  }]
}

And more about this you can read this example Nested_Results_for_Collection

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

2 Comments

i'll give it a try in a moment
I guess I need to practice SQL more... thanks for the answer

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.