spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

AOT-generated derived query for GreaterThanEqual calls non-existent `Criteria.greaterThanEquals(...)` instead of `greaterThanOrEquals(...)`

Open RaresMusea opened this issue 3 months ago • 2 comments

Component: spring-data-jdbc (AOT repository generation)

Affected version: 4.1.0 (also present in 4.0.5 and 3.5.13's Criteria/CriteriaStep API, so the mismatch is not new to 4.1.0, only the AOT codegen path introduced in the 3.x/4.x AOT support surfaces it as a compile error)

Related environment / tech stack:

  • Spring Boot 4.1.0
  • Java 25
  • Gradle
  • spring-boot-starter-data-jdbc, running ./gradlew compileAotJava / jibDockerBuild (or any other build task that triggers Spring AOT processing)
  • OS: MacOS Sequoia v15.7.8, but the issue was also observed within the CI / CD pipelines, which were running on Linux distributions

Summary

A repository method using the derived query keyword GreaterThanEqual fails to compile when Spring AOT generates the repository implementation, because the generated source calls Criteria.where(...).greaterThanEquals(...), but Criteria$CriteriaStep only declares greaterThanOrEquals(Object) and there is no greaterThanEquals() method anywhere in spring-data-relational.

The sibling keyword LessThanEqual is handled correctly and generates .lessThanOrEquals(...), confirming this is an asymmetric bug isolated to the GreaterThanEqual/GTE case.

Steps to reproduce

  1. Define a Spring Data JDBC repository with a derived query using GreaterThanEqual,like the following:
public interface PaymentRepository extends ListCrudRepository<PaymentEntity, Long> {
    List<PaymentEntity> findAllByValueDateGreaterThanEqual(LocalDate from);
}
  1. Enable Spring AOT processing (run ./gradlew compileAotJava, or any other task that depends on it such as bootBuildImage/ jib)
  2. Observe the generated Impl__AotRepository.java under build/generated/aotSources/....

Actual behavior

The generated implementation contains:

public List<PaymentEntity> findAllByValueDateGreaterThanEqual(LocalDate from) {
    Criteria criteria = Criteria.where("valueDate").greaterThanEquals(from);
    ...
}

This fails to compile:

error: cannot find symbol
    Criteria criteria = Criteria.where("valueDate").greaterThanEquals(from);
                                                   ^
  symbol:   method greaterThanEquals(LocalDate)
  location: interface CriteriaStep

For comparison, the sibling method using LessThanEqual compiles fine and correctly generates:

public List<PaymentEntity> findAllByValueDateLessThanEqual(LocalDate to) {
    Criteria criteria = Criteria.where("valueDate").lessThanOrEquals(to);
    ...
}

Expected behavior

The GreaterThanEqual keyword should generate .greaterThanOrEquals(from), matching the actual method name declared on Criteria$CriteriaStep, like:

public abstract Criteria lessThan(Object);
public abstract Criteria lessThanOrEquals(Object);
public abstract Criteria greaterThan(Object);
public abstract Criteria greaterThanOrEquals(Object);

Assumed root cause

Within the JdbcCodeBlocks.java class (org.springframework.data.jdbc.repository.aot), right inside the appendCriteria() method, the comparator-to-codeblock switch has:

case LTE -> builder.add(".lessThanOrEquals($L)", renderPlaceholder(value));
case GT  -> builder.add(".greaterThan($L)", renderPlaceholder(value));
case GTE -> builder.add(".greaterThanEquals($L)", renderPlaceholder(value));   // <-- missing "Or"

LTE correctly emits lessThanOrEquals(), but GTE emits greaterThanEquals() rather than greaterThanOrEquals(). This is a plain typo/inconsistency between the two cases.

Note the non-AOT runtime path (CriteriaFactory.java), used by the reflective/proxy-based query derivation) already gets this right, mapping GREATER_THAN_EQUAL to greaterThanOrEquals. Only the AOT source-generation path found within the JdbcCodeBlocks class has the bug, so it only breaks builds that opt into AOT processing.

Suggested fix:

- case GTE -> builder.add(".greaterThanEquals($L)", renderPlaceholder(value));
+ case GTE -> builder.add(".greaterThanOrEquals($L)", renderPlaceholder(value));

Impact on our end

Any Spring Data JDBC repository with a derived query method using GreaterThanEqual (like the exemplified findAllByFooGreaterThanEqual(), findByBarGreaterThanEqualOrderByBaz(), etc.) will fail compileAotJava task and any downstream task that depends on it (native image builds, bootBuildImage, jib, verifyAot), even though the exact same query keyword works fine without AOT enabled.

Workaround

While waiting out for a potential follow-up on this, we've applied to following workaround to our affected repository methods, by replacing out the derived-query method with an explicit @Query, like:

@Query("""
        SELECT * FROM payment
        WHERE value_date >= :from
        """)
List<PaymentEntity> findAllByValueDateGreaterThanEqual(LocalDate from);

RaresMusea avatar Aug 18 '26 08:08 RaresMusea

I dug into this and can confirm the root cause and the fix.

In JdbcCodeBlocks#appendCriteria:

case LTE -> builder.add(".lessThanOrEquals($L)", renderPlaceholder(value));
case GT  -> builder.add(".greaterThan($L)", renderPlaceholder(value));
case GTE -> builder.add(".greaterThanEquals($L)", renderPlaceholder(value)); // missing "Or"

Criteria only declares greaterThanOrEquals(Object) (no greaterThanEquals), so the GTE case is the only one of the four comparators generating a call to a method that doesn't exist. The non-AOT runtime path (CriteriaFactory) already maps GREATER_THAN_EQUAL to greaterThanOrEquals correctly, so the bug is isolated to this one line in the AOT source-generation path.

I verified this locally:

  • Reverting just this line and adding a findAllByAgeGreaterThanEqual derived-query method reproduces the reported compile failure (the generated AOT fragment fails to compile, so the test's ApplicationContext fails to load).
  • Changing .greaterThanEquals($L) to .greaterThanOrEquals($L) fixes it; JdbcRepositoryContributorIntegrationTests passes (39/39) with two added tests covering GreaterThanEqual/LessThanEqual at the boundary value.

Happy to send a PR with the one-line fix plus the two regression tests — let me know if that works or if you'd prefer a different test placement.

jejes323 avatar Aug 31 '26 05:08 jejes323

Hello! Thank you very much for your involvement.

From my perspective, this solution is totally fine and will help me and my team with some of the @Query() annotations that we had to apply to many of our repository interfaces, just to have our AOT task pass 😅 So yeah, if you can add a PR with those changes, it would be great.

RaresMusea avatar Aug 31 '26 05:08 RaresMusea