Search code examples
springspring-bootspring-data-jpaspring-data

Upgrading Spring Boot 2.5.2 to 2.6.3 breaks all projection based JPA queries


When I try to upgrade to application's Spring Boot from 2.5.2 to 2.6.3 all projection based JpaRepository queries fail to be generated with the following:

......
......
......

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.misc.project.ProjectRepository.getProjectionByIdAndField(java.lang.String,java.lang.String)! null
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:96)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:113)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:254)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:87)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:102)
    ... 85 common frames omitted
Caused by: java.lang.NullPointerException: null
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:181)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:152)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.complete(JpaQueryCreator.java:59)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:217)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:92)
    ... 89 common frames omitted

Method looks like:

Optional<SomeProjection> getProjectionByIdAndField(String id, String field);

Projection is interface based like so:

public interface SomeProjection {
  Long getSomething();
}

If I write the query manually with @Query, it moves to the next projection. Is there some way I can fix this issue without having to write manually queries for all projection based JPA methods?

I cannot find any mention of this problem here: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#upgrading-from-spring-boot-25

I found they mention some improvements to projections in: https://github.com/spring-projects/spring-data-commons/wiki/Release-Train-2021.1-%28Q%29-Release-Notes#jdbc.projections

But nothing about breaking old versions.


Solution

  • This is a bug introduced in Spring Data JPA 2.6.1. Upgrade to Spring Boot 2.6.4 to get the Spring Data JPA version containing the fix.