Skip to content

Fix user-defined resultMaps being replaced when enableBaseResultMapFlag=true#950

Draft
abel533 with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-nested-query-issue
Draft

Fix user-defined resultMaps being replaced when enableBaseResultMapFlag=true#950
abel533 with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-nested-query-issue

Conversation

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown

When enableBaseResultMapFlag=true, MapperHelper.setRawSqlSourceMapper() was replacing user-configured resultMaps with the JPA-generated BaseMapperResultMap, breaking nested queries (<association select=...>, <collection select=...>) and discriminators. The bug: the only guard was resultMappings.isEmpty(), but a <resultMap> containing only a <discriminator> stores it outside resultMappings, causing it to pass the check and get replaced.

Changes

  • MapperHelper.setRawSqlSourceMapper() — tighten the replacement guard:

    // Before: only checks that resultMappings is empty
    if (rm.getResultMappings().isEmpty()) {
    
    // After: also requires the resultMap was auto-generated from this statement
    if (rm.getResultMappings().isEmpty() && rm.getId().startsWith(ms.getId() + "-")) {

    MyBatis always names auto-generated inline resultMaps with a statementId + "-" prefix (-Inline for XML resultType=, -void/-TypeName for unannotated @Select). User-defined resultMaps (XML <resultMap id="..."> or @ResultMap) have namespace-scoped IDs with no relation to any statement ID, so they never match and are left untouched.

  • New NestedQueryTest — covers three scenarios: resultMap with explicit mappings + nested select, association-only resultMap, and discriminator-only resultMap with nested selects in case maps (the last being the direct reproducer of the bug).

Copilot AI linked an issue Jul 15, 2026 that may be closed by this pull request
Only replace auto-generated inline resultMaps in setRawSqlSourceMapper.
Auto-generated maps have IDs starting with statementId + "-" (e.g. "-Inline"
for XML resultType=, "-void"/"-TypeName" for annotations without @results).
User-defined resultMaps (resultMap= or @resultMap) have unrelated IDs and
are now left intact, preserving nested queries, discriminators, etc.

Add NestedQueryTest to reproduce and verify the fix.
Copilot AI changed the title [WIP] Fix nested query issue after upgrading to 5.0.0 Fix user-defined resultMaps being replaced when enableBaseResultMapFlag=true Jul 15, 2026
Copilot AI requested a review from abel533 July 15, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

从3.5.3升级到5.0.0后,嵌套查询失效

2 participants