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

Query methods returning Maps shouldn't return null if no result is present [DATACMNS-917] #1372

Closed
spring-projects-issues opened this issue Sep 23, 2016 · 9 comments
Assignees
Labels
in: repository type: bug

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

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 the Map is null instead of empty. In my interpretation if the underlying ResultList is empty then the map is of course null, but could be initiated to empty. Also Optional<Map<Key, Value>> throws an IllegalStateException


Issue Links:

  • DATACMNS-1337 Custom Spring Data Repository returns LinkedHashMap instead of null

Referenced from: commits 4861295, 7ae5b66, 3811ce5

Backported to: 1.12.4 (Hopper SR4), 1.11.6 (Gosling SR6)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

Oliver Drotbohm commented

Good one! Do you have an example project that we could check a potential fix against?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

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

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

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 getResultList(), it doesn't seem to ever return null in the first place. We actually only return null from a query execution that throws a NoResultException, which — AFAICT — is only thrown for executions that use getSingleResult(), which seems to be a weird thing to combine with a Map as return type.

Again, any bit of more context would be incredibly helpful

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

Daniel Dacar commented

spring-data-jpa / 1.10.2.RELEASE

I can reproduce the problem with this simple case

@Query("SELECT new map(id, id) FROM Entity")
Map<BigDecimal, BigDecimal> findMap();
  • if there are values in the table the mapping is correct
  • if there are no values in the table, the Map is null

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

Oliver Drotbohm commented

Thanks, that helps!

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

Daniel Dacar commented

I simplified the example a bit

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

Oliver Drotbohm commented

That's fixed in Spring Data Commons. We now return empty Map instances for null values in the final result conversion step. We unfortunately can't do it earlier as otherwise the wrapping into an Optional would break. However, I am not too sure wrapping a Map into an Optional makes much sense at all.

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)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

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.
Thank you for the suggestion, the query in my example was just a simplification for your quick understanding of the bug. What I was trying to do was getting a Map for this query

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

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 23, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository type: bug
Projects
None yet
Development

No branches or pull requests

2 participants