Spring Boot 2.0.0 M7 更新后,@Query 中未读取 SpEL
SpEL are not read in @Query after Spring Boot 2.0.0 M7 update
我开发了 Spring Boot 2.0.0 M3 应用程序。一切正常。但是在更新到 Spring Boot 2.0.0 M7(或者最近更新到 Spring Boot 2.0.0 M5)之后,我的 @Query
中的 SpEL
不再正确编译。例如
@Query("SELECT m FROM com.mypackage.models.Model m WHERE m.status IN
(:#{T(com.mypackage.utils.Constants).SPECIAL_STATUSES})")
public List<Model> findModelWithSpecialStatuses();
我得到以下异常
org.springframework.dao.InvalidDataAccessApiUsageException: Unknown parameter name : __$synthetic$__1; nested exception is java.lang.IllegalArgumentException: Unknown parameter name : __$synthetic$__1
Caused by: java.lang.IllegalArgumentException: Unknown parameter name : __$synthetic$__1
at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:208) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:486) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:104) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter(QueryParameterSetter.java:99) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.ParameterBinder.lambda$bind[=12=](ParameterBinder.java:57) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_131]
at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080) ~[na:1.8.0_131]
at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:57) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateCountQuery(AbstractStringBasedJpaQuery.java:108) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createCountQuery(AbstractJpaQuery.java:228) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.count(JpaQueryExecution.java:199) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.lambda$doExecute[=12=](JpaQueryExecution.java:193) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.support.PageableExecutionUtils.getPage(PageableExecutionUtils.java:62) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:192) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:89) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:127) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:116) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:597) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:580) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.2.RELEASE.jar:5.0.2.RELEASE]
... 115 common frames omitted
(:#{T(com.mypackage.utils.Constants).SPECIAL_STATUSES})
- 是 List
的 Object
s
事实证明,Spring Boot
的较新版本中的 Hibernate
优化了它的查询,因此一些参数不再可见。我找到的最快的解决方案是简单地检查 @Query
中 not null
的那些参数
我开发了 Spring Boot 2.0.0 M3 应用程序。一切正常。但是在更新到 Spring Boot 2.0.0 M7(或者最近更新到 Spring Boot 2.0.0 M5)之后,我的 @Query
中的 SpEL
不再正确编译。例如
@Query("SELECT m FROM com.mypackage.models.Model m WHERE m.status IN
(:#{T(com.mypackage.utils.Constants).SPECIAL_STATUSES})")
public List<Model> findModelWithSpecialStatuses();
我得到以下异常
org.springframework.dao.InvalidDataAccessApiUsageException: Unknown parameter name : __$synthetic$__1; nested exception is java.lang.IllegalArgumentException: Unknown parameter name : __$synthetic$__1
Caused by: java.lang.IllegalArgumentException: Unknown parameter name : __$synthetic$__1
at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:208) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:486) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:104) ~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
at org.springframework.data.jpa.repository.query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter(QueryParameterSetter.java:99) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.ParameterBinder.lambda$bind[=12=](ParameterBinder.java:57) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_131]
at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080) ~[na:1.8.0_131]
at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:57) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateCountQuery(AbstractStringBasedJpaQuery.java:108) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createCountQuery(AbstractJpaQuery.java:228) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.count(JpaQueryExecution.java:199) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.lambda$doExecute[=12=](JpaQueryExecution.java:193) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.support.PageableExecutionUtils.getPage(PageableExecutionUtils.java:62) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:192) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:89) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:127) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:116) ~[spring-data-jpa-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:597) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:580) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.2.RELEASE.jar:5.0.2.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.2.RELEASE.jar:5.0.2.RELEASE]
... 115 common frames omitted
(:#{T(com.mypackage.utils.Constants).SPECIAL_STATUSES})
- 是 List
的 Object
s
事实证明,Spring Boot
的较新版本中的 Hibernate
优化了它的查询,因此一些参数不再可见。我找到的最快的解决方案是简单地检查 @Query
not null
的那些参数