Skip to content

SpEL support for @Query annotation [DATAJDBC-397] #619

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

Closed
spring-projects-issues opened this issue Jul 16, 2019 · 1 comment
Closed

SpEL support for @Query annotation [DATAJDBC-397] #619

spring-projects-issues opened this issue Jul 16, 2019 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

sopeLi opened DATAJDBC-397 and commented

 

@Modifying
@Query(value = "update orders o set o.status = #{#order.status}")
Integer updateOrder(@Param("order") Order order);

 

I want to get the parameters of the order by means of order entity then update.
 


Reference URL: https://stackoverflow.com/questions/57040030/spring-data-jdbc-use-query-comments-how-to-match-the-parameters-registered-for

Issue Links:

  • DATAJDBC-468 Supporting SQL Function around inserting value

  • DATAJDBC-526 Ability to change default sql type mappings

  • DATAJDBC-605 Parameter concatenation in queries using named parameters

5 votes, 4 watchers

@spring-projects-issues
Copy link
Author

Mauro Molinari commented

It was a surprise to discover that, contrary to for instance Spring Data JPA, Spring Data JDBC does not support SpEL in @Query annotation value.

In particular, this doesn't work, even if Spring Security is configured correctly:

@Query("SELECT * FROM Credit WHERE customer = ?#{ principal.id }")
Optional<Credit> findByCurrentCustomer();

Documentation at https://docs.spring.io/spring-security/site/docs/5.3.2.RELEASE/reference/html5/#data
is not very clear, because it talks about "Spring Data" generally.
However if I try to execute the above query the following exception is thrown:

org.springframework.dao.InvalidDataAccessApiUsageException: SQL [SELECT * FROM Credit WHERE customer = ?#{ principal.id }]: given 1 parameters but expected 0
	at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.<init>(PreparedStatementCreatorFactory.java:220) ~[spring-jdbc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.<init>(PreparedStatementCreatorFactory.java:198) ~[spring-jdbc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.jdbc.core.PreparedStatementCreatorFactory.newPreparedStatementCreator(PreparedStatementCreatorFactory.java:172) ~[spring-jdbc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator(NamedParameterJdbcTemplate.java:419) ~[spring-jdbc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.getPreparedStatementCreator(NamedParameterJdbcTemplate.java:392) ~[spring-jdbc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:236) ~[spring-jdbc-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.data.jdbc.repository.query.AbstractJdbcQuery.lambda$singleObjectQuery$1(AbstractJdbcQuery.java:115) ~[spring-data-jdbc-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.data.jdbc.repository.query.StringBasedJdbcQuery.execute(StringBasedJdbcQuery.java:85) ~[spring-data-jdbc-2.0.1.RELEASE.jar:2.0.1.RELEASE]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at com.sun.proxy.$Proxy115.findByCurrentCustomer(Unknown Source) ~[?:?]
	at it.dcssrl.credito.controller.CreditController.getBalance(CreditController.java:28) ~[main/:?]
[...]

suggesting Spring Data JDBC is not recognizing the SpEL expression as a valid resolver for the parameter

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 a pull request may close this issue.

2 participants