Skip to content

Instantly share code, notes, and snippets.

@itzg
Created March 3, 2024 20:45
Show Gist options
  • Save itzg/d13c5bcccdf07c4aabf50b3e8dde3975 to your computer and use it in GitHub Desktop.
Save itzg/d13c5bcccdf07c4aabf50b3e8dde3975 to your computer and use it in GitHub Desktop.
Spring Data JDBC SpelEvaluationException property or field cannot be found

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)

Solution

:#{#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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment