question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

resultMap can not mapping <result column="d.id" property="id"/> when the field "fullColumnNameToIndex" contains “d.id”

See original GitHub issue

MyBatis version

3.5.5

Database vendor and version

mysql 8.0

Test case or example project

as follow,resultMap has result column = d.id Mapping sysDepartment.Id

<resultMap id="listMap" type="SysUser ">
        <result column="u.id" property="id"/>
        <result column="DepartmentId" property="departmentId"/>
        <association property="sysDepartment" javaType="SysDepartment">
            <result column="d.id" property="id"/>
        </ association>
</ resultMap>
<select id="list" resultMap="listMap">
        select u.* ,d.* from SysUser u left join SysDepartment d on d.Id = u.departmentId
</select>

as follow,in source code class DefaultColumnDefinition, the fullColumnNameToIndex field contains d.id column.so Under normal conditions, column = d.id can Mapping sysDepartment.Id. 6

as follow,in class method DefaultResultSetHandler.createRowKeyForMappedProperties,because mappedColumnNames not contains d.id,so code final Object value = th.getResult(rsw.getResultSet(), column) can not excute. this cause column = d.id can not Mapping sysDepartment.Id. 5

Expected result

resultMap has result **column = d.id column = d.id can Mapping sysDepartment.Id

Actual result

resultMap has result **column = d.id column = d.id can not Mapping sysDepartment.Id

##how to fix DefaultResultSetHandler(class).createRowKeyForMappedProperties(method).mappedColumnNames(field) cotains d.id column just like DefaultColumnDefinition(class).findColumn(method).columnToIndexCache(field)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
g5zhu5896commented, Nov 23, 2020

Hello, @g5zhu5896 .

Column labels don’t include table names i.e. both u.id and d.id will have the same column label id in the result set. You have to use column alias to distinguish them (it’s not a MyBatis issue, but a common JDBC/SQL limitation, by the way).

select u.*, d.id as d_id from ...
<association ...>
  <id column="d_id" property="id" />

It might be better to define a separate result map for SysDepartment and reference it with columnPrefix. https://mybatis.org/mybatis-3/sqlmap-xml.html#Nested_Results_for_Association

p.s. Please use text instead of image so that I can copy & paste 🙏

but in the source code, class DefaultColumnDefinition.findColumn.fullColumnNameToIndex(used to mapping result set.) contains table names i.e. both u.id and d.id column, if the source code DefaultResultSetHandler(class).createRowKeyForMappedProperties(method).mappedColumnNames (field) also contains d.id and u.id, Column labels will be able include table names i.e. both u.id and d.id . I tried and successfully

0reactions
harawatacommented, Nov 23, 2020

I did quick test. A few drivers (mysql, mariadb, pgjdbc) support it, but others (e.g. mssql, oracle, h2, hsqldb) don’t. Closing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found