Returned list of object inside object with MyBatis

I am having trouble returning a list of objects inside another object while using MyBatis. My main object looks like this:

private Long id;

private String symbol;

private List<TypePermission> typePermissions;

      

and my matcher 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 sql query I get the following error message cannot find symbol TypePermissions

because the main SELECT is trying to select rows like TYPEPERMISSIONS, ID, SYMBOL

I have searched through the internet but did not find anything useful. Could you please help me and point out what I am doing wrong?

+3


source to share


1 answer


Please post your shot, I think it will be 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 get is something like this:



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

      

And more about this you can read this Nested_Results_for_Collection example

+6


source







All Articles