From 13d4f120f8d5ea3359dd4782edf452345562ca6c Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Fri, 20 Oct 2023 10:31:54 +0200 Subject: [PATCH] Mention usage of SpEL in queries. Closes #1566 --- .../modules/ROOT/pages/jdbc/query-methods.adoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/antora/modules/ROOT/pages/jdbc/query-methods.adoc b/src/main/antora/modules/ROOT/pages/jdbc/query-methods.adoc index 8e362100..a861c9ac 100644 --- a/src/main/antora/modules/ROOT/pages/jdbc/query-methods.adoc +++ b/src/main/antora/modules/ROOT/pages/jdbc/query-methods.adoc @@ -169,7 +169,18 @@ Properties that don't have a matching column in the result will not be set. The query is used for populating the aggregate root, embedded entities and one-to-one relationships including arrays of primitive types which get stored and loaded as SQL-array-types. Separate queries are generated for maps, lists, sets and arrays of entities. +Queries may contain SpEL expressions where bind variables are allowed. +Such a SpEL expression will get replaced with a bind variable and the variable gets bound to the result of the SpEL expression. +.Use a SpEL in a query +[source,java] +---- +@Query("SELECT * FROM person WHERE id = :#{person.id}") +Person findWithSpEL(PersonRef person); +---- + +This can be used to access members of a parameter, as demonstrated in the example above. +For more involved use cases an `EvaluationContextExtension` can be made available in the application context, which in turn can make any object available in to the SpEL. NOTE: Spring fully supports Java 8’s parameter name discovery based on the `-parameters` compiler flag. By using this flag in your build as an alternative to debug information, you can omit the `@Param` annotation for named parameters. @@ -188,6 +199,8 @@ Named queries are expected to be provided in the property file `META-INF/jdbc-na The location of that file may be changed by setting a value to `@EnableJdbcRepositories.namedQueriesLocation`. +Named queries are handled in the same way as queries provided by annotation. + [[jdbc.query-methods.customizing-query-methods]] === Customizing Query Methods