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

Spring Data JPA projection to records using native queries does not work #3394

Open
pashazadeh opened this issue Mar 14, 2024 · 7 comments
Open
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged

Comments

@pashazadeh
Copy link

I have used Interface based projection using native queries for long time, but now according to Spring Data JPA reference documentstions record-based projections must work too, but it seems it does not support custom native queries.

As proxied interfaces are not so friendly during debugging, using records would be great, but it seems the conversion does not work with records.

Can this issue be solved?

By the way I am using Spring Data 3.2.3 (latest release till now)

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

mp911de commented Mar 14, 2024

but it seems it does not support custom native queries.

Care to elaborate?
If you would like us to spend some time helping you to diagnose the problem, please spend some time describing it and, ideally, providing a minimal yet complete sample that reproduces the problem.
You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Mar 14, 2024
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Mar 21, 2024
@pashazadeh
Copy link
Author

pashazadeh commented Mar 26, 2024

Well I like to contribute, but not sure how to implement it.

I am thinking of a ConverterFactory<Tuple, Record>, but not sure how to implement it of the Record type has complex components (such as nested Records, nested interfaces, or objects).

For simple cases which Record just contains, primitives, wrappers, and simple types such as String, Date, java.time classes (anything Spring ConversionService can convert), it can be done.

@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 status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Mar 26, 2024
@pashazadeh
Copy link
Author

pashazadeh commented Mar 26, 2024

I digged more in Spring Data JPA sources in class org.springframework.data.jpa.repository.query.NamedQuery, method Class<?> getTypeToRead(ReturnedType returnedType) just returns tuple when ReturnType is an interface, and not a record, so now Hibernate returns an Object[] which is not suitable for extracting results columns by name.

Well I am using named queries, as in our project using queries in annotations is prohibited (as queries are usually so long).

@pashazadeh
Copy link
Author

pashazadeh commented Mar 26, 2024

A mismatch between using native-named-queries and native-queries within annotation, when I put the query text in annotation, the return type is now a jakarta.persistence.Tuple (it occurs for annotation parameter nativeQuery = true).

But my named query was also native (and I have @Query annotation with nativeQuery=true), but in this case the expecting type from Hibernate is not a jakarta.persistence.Tuple, as
em.createNamedQuery(queryName)
is called instead of
em.createNamedQuery(queryName, typeToRead)

@pashazadeh
Copy link
Author

pashazadeh commented Mar 27, 2024

In Hibernate you can unwrap a jakarta.persistence.Query to org.hibernate.Query and then call setTupleTransformer(NativeQueryConstructorTransformer), it converts query result to an object using constructor arguments (which is suitable for Records). But as I have no experience with EclipseLink, don't know how to do that there.

By the way Hibernate's NativeQueryConstructorTransformer does not support proper conversion, a specific implementation of TupleTransformer which uses Spring ConversionService would be much nicer.

@ah1508
Copy link

ah1508 commented Jun 3, 2024

Answers to this "problem" from Spring team here: #2757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants