There seems to be a typo in the Spring Data JDBC section for @Query
since the following custom repository query with the placeholder reference :#{topic.id}
:
@Query("SELECT * FROM activity" +
" WHERE topic = :#{topic.id}" +
" AND lower(name) LIKE lower(:namePattern)" +
" ORDER BY name")
List<Activity> findAllNameLikeCaseInsensitive(Topic topic, String namePattern);
will result in the following exception at startup:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'topic' cannot be found on object of type 'java.lang.Object[]' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:228)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:111)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:99)
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:61)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:97)
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:114)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:273)
at org.springframework.data.repository.query.SpelEvaluator.getSpElValue(SpelEvaluator.java:88)
:#{#topic.id}
|| ||
|| |+- query method parameter
|| +- indicates that this SpEL is a parameter reference
|+- indicates that this JDBC named template placeholder is SpEL
+- indicates that this is a nemd JDBC template placeholder
So the fixed query method is
@Query("SELECT * FROM activity" +
" WHERE topic = :#{#topic.id}" +
" AND lower(name) LIKE lower(:namePattern)" +
" ORDER BY name")
List<Activity> findAllNameLikeCaseInsensitive(Topic topic, String namePattern);