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

Allow returning Map from the repository methods #3636

Closed
mipo256 opened this issue Oct 7, 2024 · 3 comments
Closed

Allow returning Map from the repository methods #3636

mipo256 opened this issue Oct 7, 2024 · 3 comments
Assignees
Labels
type: documentation A documentation update

Comments

@mipo256
Copy link

mipo256 commented Oct 7, 2024

I've not found any similar issue, so I'm creating a new one.

Problem:

The problem is that sometimes native queries are quite complicated, and it is possible that they would return unknown at declaration time number of columns. I'm not saying it is a great way of doing things, but that's just what we have in our case, for instance.

And in this case we cannot really return any kind of projection, since we do not know anything about the structure of the result set being returned at compile time.

Solution (Proposal):

I think it makes sense to consider allowing returning the Map from the repository query method, like:

@Query(value = "...", nativeQuery=true)
Map<String, Object> findSomethingComplex();

In this case, the key is supposed to be the column name, and the value is just a raw column value.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 7, 2024
@christophstrobl
Copy link
Member

Thank you for getting in touch.
Repository finder methods in data-jpa using Map or List<Map> as return type will give you something called a TupleBackedMap delegating calls to an underlying jakarta.persistence.Tuple. So actually the outlined code from above should already work as expected.

I had a look at our reference documentation but could not spot anything mentioning this behaviour. So, unless I'm missing something here, this seems to be a documentation issue.

@christophstrobl christophstrobl added type: documentation A documentation update status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 8, 2024
@christophstrobl christophstrobl self-assigned this Oct 8, 2024
@mipo256
Copy link
Author

mipo256 commented Oct 8, 2024

Hey @christophstrobl!
That sounds right. Would it also work for the methods where we expect a single result from the database? Like single Map, not a List.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 8, 2024
@christophstrobl
Copy link
Member

Yes, you could have something like:

@Query(value = "SELECT * from book", nativeQuery=true)
List<Map<String, Object>> findSomething();

@Query(value = "SELECT * from book WHERE id = 1", nativeQuery=true)
Map<String, Object> findSomethingSingle();

I'll update the documentation.

mp911de added a commit that referenced this issue Oct 17, 2024
Mention restrictions.

See #3636
Original pull request: #3637
mp911de pushed a commit that referenced this issue Oct 17, 2024
Closes #3636
Original pull request: #3637
mp911de added a commit that referenced this issue Oct 17, 2024
Mention restrictions.

See #3636
Original pull request: #3637
mp911de pushed a commit that referenced this issue Oct 17, 2024
Closes #3636
Original pull request: #3637
mp911de added a commit that referenced this issue Oct 17, 2024
Mention restrictions.

See #3636
Original pull request: #3637
@mp911de mp911de removed the status: feedback-provided Feedback has been provided label Oct 17, 2024
@mp911de mp911de added this to the 3.2.11 (2023.1.11) milestone Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants