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
Query methods returning Maps shouldn't return null if no result is present [DATACMNS-917] #1372
Comments
Oliver Drotbohm commented Good one! Do you have an example project that we could check a potential fix against? |
Daniel Dacar commented Sorry I don't. My use case is simple: I try to convert a group by result of two sql fields into a Map. The curent workaround is to create a List of custom entities and convert them to the Map |
Oliver Drotbohm commented Which Spring Data JPA version are you using? Also, it'd be still helpful to see what kind of query method you're actually trying to execute. As per the Javadoc of Again, any bit of more context would be incredibly helpful |
Daniel Dacar commented spring-data-jpa / 1.10.2.RELEASE I can reproduce the problem with this simple case
|
Oliver Drotbohm commented Thanks, that helps! |
Daniel Dacar commented I simplified the example a bit |
Oliver Drotbohm commented That's fixed in Spring Data Commons. We now return empty Considering your use case, have you thought of rather using a dedicated projection type so that your client code would get type safe access? interface MyProjection {
Integer getFirst();
Integer getSecond();
}
@Query("select x as first, y as second from Entity")
Optional<MyProjection> findByQuery(); That'd give you dedicated indication of the presence or absence as well as type-safe access to tie fields (and you don't have to access the map entries using String keys or the like) |
Daniel Dacar commented The Optional was just a good workaround in Java8 in case there was no fix from your community. It doesn't make sense in normal cases. Select x,y from entity group by x,y; so there are multiple results with a key and a value; and a map was perfect for it |
Oliver Drotbohm commented Ah, the grouping was what I missed. Nonetheless, would be cool if you could give the snapshots a try to see whether it fixes your issue |
Daniel Dacar opened DATACMNS-917 and commented
It's possible to return a
Map<Key, Value>
inside a custom repository using@Query
. The problem is that for no results theMap
isnull
instead of empty. In my interpretation if the underlyingResultList
is empty then the map is of coursenull
, but could be initiated to empty. AlsoOptional<Map<Key, Value>>
throws anIllegalStateException
Issue Links:
Referenced from: commits 4861295, 7ae5b66, 3811ce5
Backported to: 1.12.4 (Hopper SR4), 1.11.6 (Gosling SR6)
The text was updated successfully, but these errors were encountered: