Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException in QueryUtils.createCountQueryFor #2822

Closed
pvandenbroucke opened this issue Feb 24, 2023 · 7 comments
Closed

NullPointerException in QueryUtils.createCountQueryFor #2822

pvandenbroucke opened this issue Feb 24, 2023 · 7 comments
Labels
status: duplicate A duplicate of another issue

Comments

@pvandenbroucke
Copy link

Hello,

After upgrading the SpringBoot version from 2.7.8 to 2.7.9, I'm getting a NullPointerException :

Caused by: java.lang.NullPointerException: null
at org.springframework.data.jpa.repository.query.QueryUtils.createCountQueryFor(QueryUtils.java:607) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.StringQuery.deriveCountQuery(StringQuery.java:119) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.(AbstractStringBasedJpaQuery.java:72) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.NativeJpaQuery.(NativeJpaQuery.java:53) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:51) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:169) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:253) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103) ~[spring-data-commons-2.7.8.jar:2.7.8]

In the project, we are using several native queries with {h-schema}

Below, you can find a simplified query to reproduce the issue (similar issue happens with update/delete queries) :

@Query(value = "SELECT {h-schema}MY_SEQUENCE.nextval FROM dual", nativeQuery = true)
Long getNextVal();

Regards,

Pierrick

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 24, 2023
@A1exL
Copy link

A1exL commented Feb 24, 2023

Have the same exception with update queries. As a workaround I had to manually set countQuery.

@Modifying @Query(countQuery = "select 1", value = "update some_table set some_column = :theValue", nativeQuery = true) void updateSomething(@Param("theValue") String theValue);

Here is the line 607 from org.springframework.data.jpa.repository.query.QueryUtils:
if (nativeQuery && (variable.contains(",") || "*".equals(variable))) {

variable is null, so got NPE in variable.contains(",")

@cmdjulian
Copy link

cmdjulian commented Feb 24, 2023

For me it's the same, having a query using a cte:

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query(
    nativeQuery = true,
    value = """
        WITH some_cte AS (UPDATE some_table2 SET some_id = NULL, some_field = FALSE WHERE id = :id)
        DELETE FROM some_table WHERE id = :id"""
)
override fun deleteById(id: UUID)

I'm using Spring Boot 2.7.8 and this happens when updating to 2.7.9

@christian-kirschnick
Copy link

Same on our sider after updating from boot 3.0.2 to 3.0.3.

Affected query:

  @Query(value = "SELECT nextval('sequence')", nativeQuery = true)
  Integer generateSequence();

@ChristianCiach
Copy link

ChristianCiach commented Feb 24, 2023

Duplicate of #2812

This is also a stickied issue at https://github.com/spring-projects/spring-boot/issues

Maybe the issue should be stickied here, too, until a fixed version is released. Otherwise these duplicated issues will just pile up.

@martijndebruijn
Copy link

martijndebruijn commented Feb 27, 2023

I have the same issue with a native delete query. The delete statement returns the number of deleted records

@Transactional(TxType.REQUIRES_NEW)
@Modifying
@Query(
   value = "delete top(:batchSize) from myTable where sent = 1 and creation_date < :creationDate",
    nativeQuery = true
)
int deleteByCreationDateBeforeAndSentTrue(@Param("batchSize") int batchSize, @Param("creationDate") LocalDateTime creationDate);

Error:

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:607) ~[spring-data-jpa-2.7.8.jar:2.7.8]
	at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49) ~[spring-data-jpa-2.7.8.jar:2.7.8]

@TomKrauss
Copy link

Same NPE in my case:

    @Modifying
    @Query(value = "truncate table {h-schema}basket_position", nativeQuery = true)
    void deleteAllPositions();

@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 27, 2023
@mp911de
Copy link
Member

mp911de commented Feb 27, 2023

This is a duplicate of #2812. The release to fix the issue is scheduled to be released in about two weeks.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

9 participants