Exclude declared generated key columns from SimpleJdbcInsert#37014
Open
junhyeong9812 wants to merge 1 commit into
Open
Exclude declared generated key columns from SimpleJdbcInsert#37014junhyeong9812 wants to merge 1 commit into
junhyeong9812 wants to merge 1 commit into
Conversation
TableMetaDataContext.reconcileColumnsToUse() filtered generated key columns out of the auto-discovered column list, but returned declared columns verbatim when usingColumns() was used together with usingGeneratedKeyColumns() for the same column. This left the insert SQL (which filters generated keys separately in createInsertString()) out of sync with the bound values and types arrays (which iterate the unfiltered declared column list), causing a parameter count mismatch at execution time. Signed-off-by: junhyeong9812 <[email protected]>
f47278b to
15d9e57
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
TableMetaDataContext.reconcileColumnsToUse()excludes generated key columns from the auto-discovered column list, but when columns are declared explicitly viaSimpleJdbcInsert.usingColumns(...), a column also declared viausingGeneratedKeyColumns(...)was not excluded from that declared list. This leaves the generated insert SQL out of sync with the values/types arrays bound to it, causing a parameter count mismatch at execution time whenever a column is declared both ways.Problem
usingColumns("id", "name")combined withusingGeneratedKeyColumns("id"):returns
["id", "name"]unfiltered whendeclaredColumnsis non-empty, while the auto-discovery branch below it already filters out generated key names.createInsertString()independently filtersgeneratedKeyNameswhen building the SQL, so the insert statement only gets a placeholder forname:but
matchInParameterValuesWithInsertColumns(...)andcreateInsertTypes()both iterate the unfilteredtableColumns(= the declared columns, size 2), producing values/types arrays of size 2. The placeholder count (1) and the bound value count (2) diverge, and the JDBC driver rejects execution with a parameter index error.Fix
Apply the same generated-key filter to the declared-columns branch that the auto-discovery branch already uses:
This makes
tableColumns— the single source consumed bycreateInsertString(),createInsertTypes(), andmatchInParameterValuesWithInsertColumns(...)— consistently exclude generated key columns regardless of whether they came from auto-discovery or an explicitusingColumns(...)declaration. Added regression tests covering the partial-overlap, full-overlap (declared columns consist entirely of generated keys), and case-insensitive overlap cases.Note on impact
This changes behavior for the specific combination of declaring a column via both
usingColumns(...)andusingGeneratedKeyColumns(...)— previously that combination always failed at execution time, so no working code should depend on the old behavior. That said, it is a behavior change to aprotectedextension point (reconcileColumnsToUse), so I want to flag it explicitly in case there's a reason to prefer a different approach (e.g. rejecting the overlap explicitly instead of silently excluding it) — happy to adjust if so.