Repository: spring-projects/spring-data-jpa

Issue #2812: `NullPointerException` in version 3.0.2 when using modifying native queries or `SELECT` queries that Spring Data cannot parse 
Created: 2023-02-20T13:47:16Z
Commit 64b5a22 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

Duplicate issue #2834: NullPointerException for stored procedure call using @Query
Created: 2023-03-02T15:34:08Z
I am migrating a project form spring-boot 2.7.5 to 3.0.3 (spring-data-jpa:3.0.2).
There is the below working stored procedure call:
public interface PaymentMessageStatusRepository extends JpaRepository<PaymentMessage, Integer> {

@Query(value = "CALL set_swf(:sourceId, :description)", nativeQuery = true)
    void saveMessageHistory(Integer sourceId, String description);

}


It has stopped working for spring-data-jpa:3.0.2 and the below NullPointerException is thrown:
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) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.StringQuery.deriveCountQuery(StringQuery.java:111) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:82) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.NativeJpaQuery.<init>(NativeJpaQuery.java:58) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:53) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:170) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:252) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:95) ~[spring-data-jpa-3.0.2.jar:3.0.2]
                at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:111) ~[spring-data-commons-3.0.2.jar:3.0.2]
                ... 120 common frames omitted

It is the same for other stored procedure calls in this project.
The workaround is adding countQuery inside the @query, but I believe that throwing NullPointerException was not intentional in such case.

Closing comment (2023-03-02T15:47:47Z): Looks like a duplicate of #2812, which has been patched. You can either grab the snapshot of wait for our patch release scheduled for tomorrow.