-
Couldn't load subscription status.
- Fork 377
DATAJDBC-397 - Experimental support for SpEL inside @Query annotations. #229
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your pull request. Having SpEL support makes sense. I left a few comments since we have already utilities in place for SpEL query analysis.
| @@ -0,0 +1,295 @@ | |||
| package org.springframework.data.jdbc.repository.query.parameter; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spring Data contains a utility class from Commons (org.springframework.data.repository.query.SpelQueryContext) that allows analysis of SpEL queries and extraction of parameter bindings.
It would be used along the lines of:
List<ParameterBinding> parameterBindings = new ArrayList<>(); SpelQueryContext queryContext = SpelQueryContext.of((counter, expression) -> { String parameterName = String.format("__synthetic_%d__", counter); parameterBindings.add(new ParameterBinding(parameterName, expression)); return parameterName; }, String::concat); SpelQueryContext.SpelExtractor parsed = queryContext.parse(query);which makes this class superfluous.
| * TODO This class comes from Spring Data JPA org.springframework.data.jpa.repository.query.StringQuery and should be probably moved to Spring Data Commons. | ||
| * @author Christopher Klein | ||
| */ | ||
| public class ParameterBindings { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class could be simplified into something like:
class ParameterBinding { private final String parameterName; private final String expression; private ParameterBinding(String parameterName, String expression) { this.expression = expression; this.parameterName = parameterName; } String getExpression() { return expression; } String getParameterName() { return parameterName; } } | /* | ||
| * (non-Javadoc) | ||
| * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[]) | ||
| * |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not reformat parts of the code that shall remain untouched. You can find our formatter settings for Eclipse (IntelliJ IDEA via Eclipse Formatter Plugin) at https://github.com/spring-projects/spring-data-build/tree/master/etc/ide.
| Would really like to see SpEL support on Query-annotation here. Can this PR be finalized as is ? |
Constructs like the following work now. ``` @query("select u from User u where u.firstname = :#{#customer.firstname}") List<User> findUsersByCustomersFirstname(@param("customer") Customer customer); ``` Closes #619 Original pull request #229 See https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions
| I rebased this, applied Marks comments and merged it with main. |
| @schauder |
| @peteraramaldes This went into 3.0.0-RC1. It is therefore part of all 3.x releases after that including the 3.0.0 GA release. |
This PR tries to add experimental support to Spring Data JDBC. I've been able to use this PR with Spring Security, As described in https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions you can now use something like
Some things I like to discuss, because I don't want to do any stupid things here:
ParameterBindingsandParameterBindingParsercome from the Spring Data JPA project. At the moment they are both copy&pasted from SD-JPA and have public access modifiers. Both classes should probably be moved to Spring Data Commons.Thank you for taking the time to look into this PR!
Closes #619