spring-data-jpa: `NullPointerException` in version 3.0.2 when using modifying native queries or `SELECT` queries that Spring Data cannot parse
Commit 64b5a22a24b42e8660f441897b001e4b594fff1c introduced a null pointer exception in line 620 of QueryUtils. If a native query is not a select, but, for example, a delete, “variable” will be null, leading to said exception.
Example code in repository:
@Modifying
@Query(value = "delete from some_table where id in :ids", nativeQuery = true)
void deleteStuffFromSomeTable(@Param("ids") Collection<UUID> ids);
Relevant part of the trace:
Caused by: java.lang.NullPointerException: Cannot invoke "String.contains(java.lang.CharSequence)" because "variable" is null
at org.springframework.data.jpa.repository.query.QueryUtils.createCountQueryFor(QueryUtils.java:620)
at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49)
at org.springframework.data.jpa.repository.query.StringQuery.deriveCountQuery(StringQuery.java:111)
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:82)
at org.springframework.data.jpa.repository.query.NativeJpaQuery.<init>(NativeJpaQuery.java:58)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:53)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:170)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:252)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:111)
... 79 more
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 21
- Comments: 32 (4 by maintainers)
Commits related to this issue
- Delay count query derivation. We now delay the count query creation to the actual time when we need the count query to avoid query creation of invalid queries (e.g. count queries for DELETE or UPDATE... — committed to spring-projects/spring-data-jpa by mp911de a year ago
- Fix NullPointer when deriving a count query for non-SELECT statements. Closes #2812 — committed to spring-projects/spring-data-jpa by mp911de a year ago
- Delay count query derivation. We now delay the count query creation to the actual time when we need the count query to avoid query creation of invalid queries (e.g. count queries for DELETE or UPDATE... — committed to spring-projects/spring-data-jpa by mp911de a year ago
- Delay count query derivation. We now delay the count query creation to the actual time when we need the count query to avoid query creation of invalid queries (e.g. count queries for DELETE or UPDATE... — committed to spring-projects/spring-data-jpa by mp911de a year ago
- Fix NullPointer when deriving a count query for non-SELECT statements. Closes #2812 — committed to spring-projects/spring-data-jpa by mp911de a year ago
- Delay count query derivation. We now delay the count query creation to the actual time when we need the count query to avoid query creation of invalid queries (e.g. count queries for DELETE or UPDATE... — committed to spring-projects/spring-data-jpa by mp911de a year ago
- Fix NullPointer when deriving a count query for non-SELECT statements. Closes #2812 — committed to spring-projects/spring-data-jpa by mp911de a year ago
- :sparkles: NamedLockStockService 문제 해결 nativeQuery = true 사용시 빈 초기화 시점에 Null Pointer Exception 발생 https://github.com/spring-projects/spring-data-jpa/issues/2812 이슈 참고 하여 3.0.1버전으로 다운그레이드후 해결 — committed to waterfogSW/Cucurrent-Solutions by waterfogSW a year ago
- :sparkles: NamedLockStockService 문제 해결 nativeQuery = true 사용시 빈 초기화 시점에 Null Pointer Exception 발생 https://github.com/spring-projects/spring-data-jpa/issues/2812 이슈 참고 하여 3.0.1버전으로 다운그레이드후 해결 — committed to waterfogSW/Cucurrent-Solutions by waterfogSW a year ago
- feat: Named Lock 활용하여 동시성 이슈 해결 - Spring Data JPA NPE 이슈로 버전 변경 (https://github.com/spring-projects/spring-data-jpa/issues/2812) — committed to alicesister1/stock by kyunghee-yang a year ago
- update spring 3.0.4, fixes JPA NPE in previous release see https://github.com/spring-projects/spring-data-jpa/issues/2812 — committed to tillkuhn/angkor by tillkuhn a year ago
- downgrade spring-boot is due to https://github.com/spring-projects/spring-data-jpa/issues/2812 — committed to saveourtool/save-cloud by nulls a year ago
- downgrade spring-boot is due to https://github.com/spring-projects/spring-data-jpa/issues/2812 — committed to saveourtool/save-cloud by nulls a year ago
- downgrade spring-boot is due to https://github.com/spring-projects/spring-data-jpa/issues/2812 — committed to saveourtool/save-cloud by nulls a year ago
- downgrade spring-boot is due to https://github.com/spring-projects/spring-data-jpa/issues/2812 (#1941) — committed to saveourtool/save-cloud by nulls a year ago
- workaround for https://github.com/spring-projects/spring-data-jpa/issues/2812 — committed to springdoc/springdoc-openapi by bnasslahsen a year ago
The next round of releases is scheduled for in 4 weeks, see https://calendar.spring.io/.
As suggested above, we recommend downgrading to Spring Data JPA 3.0.1 as the 3.0.2 release shipped with a few changes only.
Same with an UPDATE query in 2.7.9:
@otavioprado You don’t even need that full import, this should be enough:
If you’re on Spring Boot 3, you can upgrade to Spring Boot 3.0.4, which was just released.
If you use Spring Boot 2.7.9: Spring Data BOM 2021.2.9 has been released, with Spring Data 2.7.9. This will be included in Spring Boot 2.7.10 (scheduled for March 23), but you can temporarily override
spring-data-bom.versionto 2021.2.9 for now.I am surprised there was no test that could have caught this.
In my case, the NPE occurs querying a sequence value.
@ChristianCiach please dont take it wrong we love spring and community behind it. This just caught us off guard which never affected us before in this manner. When i meant i was surprised it just expressed the feeling nothing more than that.
@SabareeshGC I usually don’t find these kinds of comments very productive. But I agree that it is fair to be a bit surprised that a project of this size did not contain a single unit test that just happens to execute a mutating native query. But, well, now there is one 😃
This also once again shows the importance of static code analysis. Eclipse (with my rather strict compiler settings) immediate marked the buggy line as a potential NPE, and tools like NullAway would surely do the same. Seeing that there are nullable annotations available in
org.springframework.lang, maybe accidents like these could be prevented in the future by utilizing static code analysis.We just skip this release for both Spring Boot 2.7.9 and 3.0.3 - the work it takes “fixing” the NPE for us is not worth it.
Workaround for people using Gradle if you don’t want to downgrade Spring Boot altogether:
Same here, with non CRUD native query (spring-jpa 2.7.8)
Found a workaround by adding a fictive
countQueryAlso affects
2.7.8A simple test for
QueryEnhancerTckTeststhat would reproduce issue:“Offending” query itself can be also:
UPDATE table_name SET column_name = a_value"BEGIN ... WHEN MATCHED THEN ... END;Works on
2.7.10. Thank you all!This has been patched in
main(3.1.0-SNAPSHOT) and backported to3.0.xand2.7.x).