Search code examples
spring-data-jdbc

"java.lang.IllegalArgumentException: Cannot query by nested property" when using query derivation for property on nested entity


This is a follow-up question to this: Reference to another aggregate vs. sub-entity

I have the following Aggregate:

Evaluation (root)
|
|__Employee (entity)
|  |_SupervisorId (value object)
|
|__Year (value object)
|
|__ExpiryDate (value object)

I have an EvaluationRepository-Interface where i use query-derivation to read Evaluations. The method looks like this:

Set<Evaluation> findAllByYearAndExpiryDateIsNullAndEmployee_SupervisorId(Integer year, Long supervisorId);

i get this Exception:

Caused by: java.lang.IllegalArgumentException: Cannot query by nested property: employee.supervisorId
    at org.springframework.data.jdbc.repository.query.JdbcQueryCreator.validateProperty(JdbcQueryCreator.java:142)
    at org.springframework.data.jdbc.repository.query.JdbcQueryCreator.validate(JdbcQueryCreator.java:132)
    at org.springframework.data.jdbc.repository.query.PartTreeJdbcQuery.<init>(PartTreeJdbcQuery.java:109)
    at org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JdbcQueryLookupStrategy.java:119)
    at org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JdbcQueryLookupStrategy.java:205)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103)
    ... 136 more

This is the line in JdbcQueryCreator where the exception is thrown:

if (!path.getParentPath().isEmbedded() && path.getLength() > 1) {
            throw new IllegalArgumentException(
                    String.format("Cannot query by nested property: %s", path.getRequiredPersistentPropertyPath().toDotPath()));
        }

When i write a custom query via @Query it works. What is the problem with query derivation here?


Solution

  • What is the problem with query derivation here?

    The problem is simply that this is not implemented yet for Spring Data JDBC.