-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
NPE in JpaQueryCreator when query result is projection-based interface #2408
Comments
Confirmed after updating spring-boot from 2.6.2 to 2.6.3 with Repository Code like
Causing a NPE since |
A possible workaround is to use an abstract class instead of an interface type. |
Method ReturnedType.getReturnedType() must not be used with interface. In Spring Data Commons exists test org.springframework.data.repository.query.ReturnedTypeUnitTests.detectsProjection which is explicitely requesting that getReturnedType is returning null on interfaces:
It seems that (proper?) solution of this problem will be to replace line (which will throw NPE whenever type to read is interface)
with line
|
Hi, The example above would look like this:
|
Hi guys, the exact same thing happened to me too. Repositories with closed projections break with the same error:
I'm trying to upgrade from spring-boot 2.6.2 to 2.6.3 with Java 17 but I can't because of this error. The temporary solution in my case is the same as reported by @achimgrimm, Or annotating the repository method with @Query("select t.id as id, t.name as name from Table t t.id = :id")
List<ProjectionName> findAllByProjection_Id(@Param("id") Long id); And the projection with the following structure: public interface ProjectionName {
Long getId();
Long getName();
} But it's not as interesting as before. |
With the commit for GH-2363, we introduced an unguarded call to ReturnedType.getTypeToRead(), which could return null under certain conditions. Unfortunately Spring Data JPA dod not contain a test case that triggered that scenario. This commit switches to ReturnedType.getReturnedType() which is non-nullable and lets us inspect the calls results for interfaces, which we need to create the JPA query properly. Fixes GH-2408
With the commit for GH-2363, we introduced an unguarded call to ReturnedType.getTypeToRead(), which could return null under certain conditions. Unfortunately Spring Data JPA dod not contain a test case that triggered that scenario. This commit switches to ReturnedType.getReturnedType() which is non-nullable and lets us inspect the calls results for interfaces, which we need to create the JPA query properly. Fixes GH-2408
This should be fixed now in 2.6, 2.7 and 3.0 snapshots. |
Hi, |
The fix was applied post 2.7 M2. It’s gonna be shipped with the upcoming M3. |
I'm getting this after upgrading spring-boot-starter-parent 2.7.8 -> 2.7.9, it's pulling spring-data-jpa 2.7.8 from 2.7.7 and my repository that was working previously now is having a NPE :
on line 594, this string is created and it defaults to null if there's no match:
But the null is not handled on line 607:
Should I make a new issue? Sorry, I've never had to open a ticket with Spring. I searched the open issues and there was one other but didn't seem to be the same issue. If I switch back to spring-boot 2.7.8 and do a mvn clean install it starts up just fine with no modification of my repository. |
No, this is something completely diferent. I've met problem you are talking about in version 3.0.something. Most likely your (native) query which is causing NPE is not select, but something that is updating data. For some reason, Spring Data Jpa is trying to create count query for that (modifying) query (why?), but regular expression that is used can not match query string (no select). I can only guess that authors of that part of code never expected such situation (why should modifying query have count query?) and did not checked for null. It seems that this problem has already been reported #2824 and #2812 ... and #2822 ... |
Thanks for your help. It's actually a select going into a projection-based interface. There is a join on another table and a group by, but I've verified all the fields I'm selecting, and matched them to the type, but it seems there's an unhandled null in the new code. Here's my select statement with the nativeQuery = true. I've modified the field and table names to make them generic. SELECT u.field1, u.field2, u.field3, u.field4 as myCustomField4, u.field5 as myCustomField5, u.field6 as myCustomField6
FROM "table1" u
LEFT JOIN table2 l ON l.field1 = u.field1
WHERE u.field1 = :myVariable1 AND l.field2 = :myVariable2
GROUP BY u.field1 |
Bug has been introduced in Spaing Data JPA 2.6.1 which causes NPE to be thrown in org.springframework.data.jpa.repository.query.JpaQueryCreator when query result is projection-based interface or generic class with projection-based interface as type parameter.
Attached is simple diff file that changes org.springframework.data.jpa.repository.sample.UserRepository to demonstrate such behaviour.
While testing, if noticed that NPE is not thrown when line 181 in org.springframework.data.jpa.repository.query.JpaQueryCreator is changed from
Class<?> typeToRead = returnedType.getTypeToRead();
to
Class<?> typeToRead = returnedType.getReturnedType();
but I do not know is this can be proper fix or not.
bug.diff.txt
[Additional "feature" is that this bug is causing test to throw NPE's in infinte loop]
The text was updated successfully, but these errors were encountered: