Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions in NamedQuery creation lead to query derivation #3550

Closed
wvudako opened this issue Jul 31, 2024 · 1 comment
Closed

Exceptions in NamedQuery creation lead to query derivation #3550

wvudako opened this issue Jul 31, 2024 · 1 comment
Labels
type: bug A general bug

Comments

@wvudako
Copy link

wvudako commented Jul 31, 2024

When a NamedQuery is defined and the lookup is made an IllegalArgumentException is thrown when a syntax error occurs.

  • Behaviour

This error is neither logged nor in other way shown, not even in TRACE logging.

  • Expected behaviour
  1. The error is logged, at least in DEBUG
  2. When the named query attribute is set, if the named query is not found, there is no fallback, instead the error is thrown immidiately
  • My use case (2.7.18)
@Query(nativeQuery = true, name = XXX)
@NamedNativeQuery(name = XXX, query = """
...'???'...':parameter'...
""", resultSetMapping = YYY)

Error "java.lang.IllegalArgumentException: Mixing of ? parameters and other forms like ?1 is not supported!" (Stacktraces below)
Instead the named query was ignored and the query was tried to be resolved by name. This lead to an error of parsing the property from the method name ("No property 'x' found for type 'y'")
  • Stacktraces

Stacktrace of the NamedQuery Resolution

lookupFrom:156, NamedQuery (org.springframework.data.jpa.repository.query)
resolveQuery:179, JpaQueryLookupStrategy$DeclaredQueryLookupStrategy (org.springframework.data.jpa.repository.query)
resolveQuery:253, JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy (org.springframework.data.jpa.repository.query)
resolveQuery:93, JpaQueryLookupStrategy$AbstractQueryLookupStrategy (org.springframework.data.jpa.repository.query)
lookupQuery:103, QueryExecutorMethodInterceptor (org.springframework.data.repository.core.support)
lambda$mapMethodsToQuery$1:95, QueryExecutorMethodInterceptor (org.springframework.data.repository.core.support)
apply:-1, QueryExecutorMethodInterceptor$$Lambda$1507/0x0000000801484168 (org.springframework.data.repository.core.support)
accept:197, ReferencePipeline$3$1 (java.util.stream)
forEachRemaining:133, Iterator (java.util)
forEachRemaining:1061, Collections$UnmodifiableCollection$1 (java.util)
forEachRemaining:1845, Spliterators$IteratorSpliterator (java.util)
copyInto:509, AbstractPipeline (java.util.stream)
wrapAndCopyInto:499, AbstractPipeline (java.util.stream)
evaluateSequential:921, ReduceOps$ReduceOp (java.util.stream)
evaluate:234, AbstractPipeline (java.util.stream)
collect:682, ReferencePipeline (java.util.stream)
mapMethodsToQuery:97, QueryExecutorMethodInterceptor (org.springframework.data.repository.core.support)
lambda$new$0:87, QueryExecutorMethodInterceptor (org.springframework.data.repository.core.support)
apply:-1, QueryExecutorMethodInterceptor$$Lambda$1506/0x0000000801483f00 (org.springframework.data.repository.core.support)
map:260, Optional (java.util)
<init>:87, QueryExecutorMethodInterceptor (org.springframework.data.repository.core.support)
getRepository:365, RepositoryFactorySupport (org.springframework.data.repository.core.support)
lambda$afterPropertiesSet$5:323, RepositoryFactoryBeanSupport (org.springframework.data.repository.core.support)
get:-1, RepositoryFactoryBeanSupport$$Lambda$1386/0x0000000801463c70 (org.springframework.data.repository.core.support)
getNullable:231, Lazy (org.springframework.data.util)
get:115, Lazy (org.springframework.data.util)
afterPropertiesSet:329, RepositoryFactoryBeanSupport (org.springframework.data.repository.core.support)
afterPropertiesSet:144, JpaRepositoryFactoryBean (org.springframework.data.jpa.repository.support)
invokeInitMethods:1863, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
initializeBean:1800, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:620, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:542, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:335, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, AbstractBeanFactory$$Lambda$356/0x00000008004006b0 (org.springframework.beans.factory.support)
getSingleton:234, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:333, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:208, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:-1, DefaultListableBeanFactory (org.springframework.beans.factory.support)
resolveCandidate:276, DependencyDescriptor (org.springframework.beans.factory.config)
doResolveDependency:1391, DefaultListableBeanFactory (org.springframework.beans.factory.support)
resolveDependency:1311, DefaultListableBeanFactory (org.springframework.beans.factory.support)
resolveFieldValue:710, AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement (org.springframework.beans.factory.annotation)
inject:693, AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement (org.springframework.beans.factory.annotation)
inject:119, InjectionMetadata (org.springframework.beans.factory.annotation)
postProcessProperties:408, AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
populateBean:1431, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:619, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:542, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:335, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, AbstractBeanFactory$$Lambda$356/0x00000008004006b0 (org.springframework.beans.factory.support)
getSingleton:234, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:333, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:208, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:-1, DefaultListableBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:955, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:929, AbstractApplicationContext (org.springframework.context.support)
refresh:591, AbstractApplicationContext (org.springframework.context.support)
refresh:147, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:732, SpringApplication (org.springframework.boot)
refreshContext:409, SpringApplication (org.springframework.boot)
run:308, SpringApplication (org.springframework.boot)
run:1300, SpringApplication (org.springframework.boot)
run:1289, SpringApplication (org.springframework.boot)
startSpringBootApplication:115, PasServerApplication (at.valida.pas.server)
main:110, PasServerApplication (at.valida.pas.server)

Stacktrace of the actual error

result = {StackTraceElement[65]@39481} 
 0 = {StackTraceElement@39382} "org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(StringQuery.java:299)"
 1 = {StackTraceElement@39383} "org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.access$000(StringQuery.java:196)"
 2 = {StackTraceElement@39384} "org.springframework.data.jpa.repository.query.StringQuery.<init>(StringQuery.java:80)"
 3 = {StackTraceElement@39385} "org.springframework.data.jpa.repository.query.DeclaredQuery.of(DeclaredQuery.java:40)"
 4 = {StackTraceElement@39386} "org.springframework.data.jpa.repository.query.NamedQuery.<init>(NamedQuery.java:81)"
 5 = {StackTraceElement@39387} "org.springframework.data.jpa.repository.query.NamedQuery.lookupFrom(NamedQuery.java:150)"
 6 = {StackTraceElement@39388} "org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:179)"
 7 = {StackTraceElement@39389} "org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:253)"
 8 = {StackTraceElement@39390} "org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)"
 9 = {StackTraceElement@39391} "org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103)"
 10 = {StackTraceElement@39392} "org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:95)"
 11 = {StackTraceElement@39393} "java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)"
 12 = {StackTraceElement@39394} "java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)"
 13 = {StackTraceElement@39395} "java.base/java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1061)"
 14 = {StackTraceElement@39396} "java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845)"
 15 = {StackTraceElement@39397} "java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)"
 16 = {StackTraceElement@39398} "java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)"
 17 = {StackTraceElement@39399} "java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)"
 18 = {StackTraceElement@39400} "java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)"
 19 = {StackTraceElement@39401} "java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)"
 20 = {StackTraceElement@39402} "org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:97)"
 21 = {StackTraceElement@39403} "org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:87)"
 22 = {StackTraceElement@39404} "java.base/java.util.Optional.map(Optional.java:260)"
 23 = {StackTraceElement@39405} "org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:87)"
 24 = {StackTraceElement@39406} "org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:365)"
 25 = {StackTraceElement@39407} "org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:323)"
 26 = {StackTraceElement@39408} "org.springframework.data.util.Lazy.getNullable(Lazy.java:231)"
 27 = {StackTraceElement@39409} "org.springframework.data.util.Lazy.get(Lazy.java:115)"
 28 = {StackTraceElement@39410} "org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329)"
 29 = {StackTraceElement@39411} "org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144)"
 30 = {StackTraceElement@39412} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)"
 31 = {StackTraceElement@39413} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)"
 32 = {StackTraceElement@39414} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)"
 33 = {StackTraceElement@39415} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)"
 34 = {StackTraceElement@39416} "org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)"
 35 = {StackTraceElement@39417} "org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)"
 36 = {StackTraceElement@39418} "org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)"
 37 = {StackTraceElement@39419} "org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)"
 38 = {StackTraceElement@39420} "org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java)"
 39 = {StackTraceElement@39421} "org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)"
 40 = {StackTraceElement@39422} "org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391)"
 41 = {StackTraceElement@39423} "org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)"
 42 = {StackTraceElement@39424} "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710)"
 43 = {StackTraceElement@39425} "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693)"
 44 = {StackTraceElement@39426} "org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)"
 45 = {StackTraceElement@39427} "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:408)"
 46 = {StackTraceElement@39428} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)"
 47 = {StackTraceElement@39429} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)"
 48 = {StackTraceElement@39430} "org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)"
 49 = {StackTraceElement@39431} "org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)"
 50 = {StackTraceElement@39432} "org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)"
 51 = {StackTraceElement@39433} "org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)"
 52 = {StackTraceElement@39434} "org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)"
 53 = {StackTraceElement@39435} "org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java)"
 54 = {StackTraceElement@39436} "org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)"
 55 = {StackTraceElement@39437} "org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:929)"
 56 = {StackTraceElement@39438} "org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591)"
 57 = {StackTraceElement@39439} "org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)"
 58 = {StackTraceElement@39440} "org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)"
 59 = {StackTraceElement@39441} "org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409)"
 60 = {StackTraceElement@39442} "org.springframework.boot.SpringApplication.run(SpringApplication.java:308)"
 61 = {StackTraceElement@39443} "org.springframework.boot.SpringApplication.run(SpringApplication.java:1300)"
 62 = {StackTraceElement@39444} "org.springframework.boot.SpringApplication.run(SpringApplication.java:1289)"
 63 = {StackTraceElement@39445} "at.valida.pas.server.PasServerApplication.startSpringBootApplication(PasServerApplication.java:115)"
 64 = {StackTraceElement@39446} "at.valida.pas.server.PasServerApplication.main(PasServerApplication.java:110)"
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 31, 2024
@mp911de mp911de changed the title Logging of Errors and fallback when reading present NamedQuery with errors Exceptions in NamedQuery creation lead to query derivation Aug 1, 2024
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 1, 2024
@mp911de
Copy link
Member

mp911de commented Aug 1, 2024

Please learn how to properly format code and logs when reporting issues the next time. I updated the formatting here to create a more readable report.

Falling back to query derivation isn't intended, this is rather a side-effect of introducing better checks for query presence checking while the old guards have been left in place.

@mp911de mp911de closed this as completed in 8a95dba Aug 1, 2024
mp911de added a commit that referenced this issue Aug 1, 2024
We now no longer fall back to an absent query when a NamedQuery construction fails with IllegalArgumentException. IllegalArgumentException is also used by the JPA API to indicate an absent query. In other cases, where we fail with IllegalArgumentException, we fell back to query derivation as handling IAE as signal for an absent query.

We already have better query absence checks in place so we can remove the try/catch blocks in favor of the named query presence check.

Closes #3550
mp911de added a commit that referenced this issue Aug 1, 2024
We now no longer fall back to an absent query when a NamedQuery construction fails with IllegalArgumentException. IllegalArgumentException is also used by the JPA API to indicate an absent query. In other cases, where we fail with IllegalArgumentException, we fell back to query derivation as handling IAE as signal for an absent query.

We already have better query absence checks in place so we can remove the try/catch blocks in favor of the named query presence check.

Closes #3550
@mp911de mp911de added this to the 3.2.9 (2023.1.9) milestone Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants