Skip to content

Reject missing explicitly named queries#4293

Closed
014-code wants to merge 1 commit into
spring-projects:mainfrom
014-code:issue/DATAJPA-1417
Closed

Reject missing explicitly named queries#4293
014-code wants to merge 1 commit into
spring-projects:mainfrom
014-code:issue/DATAJPA-1417

Conversation

@014-code

@014-code 014-code commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

DATAJPA-1417 changes named query lookup so that repository methods explicitly declaring @Query(name = …) fail fast when the named query cannot be resolved.

Previously, if the configured named query was missing from both NamedQueries and the EntityManager, lookup could continue into query derivation. That fallback produced a misleading parameter-count error for methods whose name did not match the declared query parameters.

Changes

  • Reject unresolved explicit @Query(name = …) declarations with a QueryCreationException.
  • Keep existing fallback behavior for methods that do not explicitly declare a named query.
  • Add a regression test covering the missing explicit named query path.

Tests

  • .\mvnw.cmd -pl spring-data-jpa -Dtest=JpaQueryLookupStrategyUnitTests test
  • .\mvnw.cmd -pl spring-data-jpa -Dtest=PreprocessedQueryUnitTests test

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 30, 2026
@014-code 014-code force-pushed the issue/DATAJPA-1417 branch from 6b605fb to dd84d09 Compare June 30, 2026 06:34
Explicitly configured named queries must either be present in the configured NamedQueries or resolvable through the EntityManager. Previously, a missing @query(name = ...) declaration could fall back to query derivation and expose a misleading parameter binding error.

Closes spring-projects#1731

Signed-off-by: 林桉 <2402143478@qq.com>
@014-code 014-code force-pushed the issue/DATAJPA-1417 branch from dd84d09 to 122e481 Compare June 30, 2026 06:37
@mp911de

mp911de commented Jun 30, 2026

Copy link
Copy Markdown
Member

I fail to understand how this pull request relates to #1731. Care to elaborate? The change on its seems reasonable.

@mp911de mp911de self-assigned this Jun 30, 2026
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 30, 2026
@014-code

014-code commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

I fail to understand how this pull request relates to #1731. Care to elaborate? The change on its seems reasonable.

Thanks for calling that out. The relation is in the lookup path shown by the stack trace rather than in native SQL parameter parsing itself.

In #1731, the repository method declares only @query(name = "StopDistanceQuery"). If that named query is not visible to the EntityManager, query lookup currently returns NO_QUERY and CREATE_IF_NOT_FOUND falls back to query derivation.

That fallback is visible in the reported stack trace:

CreateIfNotFoundQueryLookupStrategy -> PartTreeJpaQuery

The parameter-count error is therefore produced while deriving findNextStopByTripIdAndPosition, not while executing the named native query. The method has three bindable parameters, while the derived query path sees only two parts from the method name.

This PR changes that failure mode so an explicitly declared @query(name = …) fails fast when the named query cannot be resolved, instead of falling back to query derivation and surfacing the misleading parameter-count error.

It does not attempt to fix the provider-level native query parameter parsing mentioned in the later update. If you consider that to be the actual scope of #1731, I can remove the Closes #1731 reference and keep this as a related query lookup improvement.

@mp911de mp911de changed the title DATAJPA-1417 - Reject missing explicitly named queries Reject missing explicitly named queries Jul 2, 2026
@mp911de

mp911de commented Jul 2, 2026

Copy link
Copy Markdown
Member

Thanks for sharing your thoughts.

I removed the relationship between the ticket and this pull request so we can treat it as a separate item. Since the change is a clear improvement, we can handle it within the scope of this PR.

With this change, a misspelled query name would silently fall back to query derivation. That is a relevant behavioral change for existing applications that currently boot successfully and would fail after this change.

@mp911de mp911de added this to the 4.2 M1 (2026.1.0) milestone Jul 2, 2026
@mp911de mp911de closed this in 930ca00 Jul 2, 2026
mp911de added a commit that referenced this pull request Jul 2, 2026
Validate existence of named count queries, update ticket references, document lookup behavior.

See #4293
@mp911de

mp911de commented Jul 2, 2026

Copy link
Copy Markdown
Member

Thank you for your contribution. That's merged and polished now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants