Skip to content

Conversation

@schakko
Copy link

@schakko schakko commented Jun 12, 2020

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

interface PersonRepository extends PagingAndSortingRepository<Person, String> { @Query("SELECT * FROM person WHERE username = :#{ principal?.username }") Person findActiveUser(); } 

Some things I like to discuss, because I don't want to do any stupid things here:

  • As I've already mentioned to @schauder on Twitter, this is a working prototype. I am pretty sure that there are too many corner cases I haven't think of or parts I am still lacking the understanding of :-(
  • Please let me know if I am misinterpreting the Contribution guidelines regarding the JavaDoc @author tag; I don't want to take credit for something I haven't contributed for.
  • ParameterBindings and ParameterBindingParser come 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

Copy link
Member

@mp911de mp911de left a 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;
Copy link
Member

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 {
Copy link
Member

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[])
*
Copy link
Member

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.

@dling
Copy link

dling commented Jan 21, 2021

Would really like to see SpEL support on Query-annotation here. Can this PR be finalized as is ?

@gregturn gregturn changed the base branch from master to main April 15, 2021 18:21
schauder pushed a commit that referenced this pull request Sep 30, 2022
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
schauder added a commit that referenced this pull request Sep 30, 2022
@schauder
Copy link
Contributor

schauder commented Sep 30, 2022

I rebased this, applied Marks comments and merged it with main.
Thanks for this PR.

@schauder schauder closed this Sep 30, 2022
@schauder schauder added the type: enhancement A general enhancement label Oct 10, 2022
@schauder schauder added this to the 3.0 RC1 (2022.0.0) milestone Oct 10, 2022
@schauder schauder removed the status: waiting-for-triage An issue we've not yet triaged label Oct 10, 2022
@peteraramaldes
Copy link

@schauder
This change is in which version? 3.0.1?

@schauder
Copy link
Contributor

@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.

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

Labels

type: enhancement A general enhancement

6 participants