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
Using manual SqlResultSetMapping with named native queries fails [DATAJPA-1280] #1610
Comments
Oliver Drotbohm commented Can you please add the actual query and the type definition of |
Eugene Nikiforov commented Hi, Oliver, sure
|
Oliver Drotbohm commented Can you confirm this works if you don't use the result set mapping in the first place? |
Eugene Nikiforov commented Could you clarify a little bit what approach I should use? I think there are several possibilities to invoke this query and I'm not sure which method you are thinking will work. I mean if I remove Also I've already tried to reimplement this code in another way
but got this exception
|
Oliver Drotbohm commented Oh, right. As you're using a DTO, you'd need a constructor expression which AFAIR is not allowed in native queries. I'll have to look into this. As a workaround, can you just switch to a projection interface exposing getters instead? That should allow the projecting conversion to work properly |
Eugene Nikiforov commented Yes, projection way now works well (It hadn't worked before your fix), I'm sure I could go forward with this approach. Nevertheless, I'm not sure, that it is perfect that JPA standard annotation doesn't work, because of some spring data assumptions |
Eugene Nikiforov commented Short update: I found another approach that currently works fine without removing and reimplementing major parts of code: We still use class projections, namedNativeQueries and SqlResultSetMapping, so the main structure would be the same as was described in issue description earlier, the only thing to change is repository function definition
so two major things should be done:
After these steps projections will work fine with |
Oliver Drotbohm commented That's useful information Eugene. Is there a chance you collect this into an – ideally Java based – example project that shows the different approaches (working / not-working) side by side? Basically a repository with two or three query methods, an entity with the queries declared as you have and a DTO class and interface? |
Eugene Nikiforov commented Sure, Oliver, when I'll finish it I'll come back |
Eugene Nikiforov commented Oliver, I added sample project (based on Java 8) https://github.com/EugeneNik/spring-data-datajpa-1280-example and provided tests with different situations. There are 2 entities with same fields and 2 projections (1 interface based and 1 class based). I used 2 entities just not to declare every kind of mapping in 1 class. Actually, I think you could notice several issues with projection mappings. If I could help you in some other ways or you'll need some more details, please, let me know |
Craig commented I believe this is the same issue reported at https://stackoverflow.com/questions/49056084/got-different-size-of-tuples-and-aliases-exception-after-spring-boot-2-0-0-rel (hopefully that's helpful information) |
Hamid Virani commented I am getting the same issue as mentioned in the stackoverflow link |
Miguel Martín-Forero commented This is happening also with version 1.11.11.RELEASE |
Igor Zboichik commented Seems to work fine, when in repo List without generic is returned (https://stackoverflow.com/a/49985099/2262442). |
Oliver Drotbohm commented This is now fixed but the original erroneous examples needs a few tweaks to work:
We've just pushed an example to our Spring Data Examples for reference. See the README for details |
Eugene Nikiforov commented Oliver Drotbohm Thank you so much. It works, but I have one question here, Do we really need the second step in future or it will be improved? (I could work on this enhancement voluntarily for the future release) Just my thoughts - when we are fetching declared Query by name it means that we will try to gather all defined NamedQueries on the entity and as far as they are defined by annotations we know is it native or not... (NamedNativeQuery vs NamedQuery), so do we really need to put this extra nativeQuery=true in Repository level to start mapping that could be start just with "annotation class". Maybe I don't cover some scenarios with this thoughts, so please, feel free to correct me |
Oliver Drotbohm commented
Unfortunately, we don't. We don't scan any JPA annotations (as named queries can also be defined in XML), we just call |
Eugene Nikiforov commented Ahh...That makes sense, sorry for inconvenience, but anyway I believe these comments will be useful for further users, who will investigate it. Thank you for clarification |
Oliver Drotbohm commented Indeed. I'll see what we can do in whenever JPA.next is going to be worked on. Would be a useful addition to the EntityManager API to be able to look that information up |
spring-projects-issues commentedMar 1, 2018
Eugene Nikiforov opened DATAJPA-1280 and commented
javax.persistence.NamedNativeQuery result couldn't properly use javax.persistence.SqlResultSetMapping declaration according to DATAJPA-1273 fix.
I'll provide Kotlin code for my example, but I think it doesn't play any role in this case.
Problem statement:
We want to execute some native query and automatically map this result to some DTO object (non-entity), we could achieve it by creating some named native query and mapping it through SqlResultSetMapping (Hibernate successfully parse all this statements and do it as per JPA standard):
Code that worked in Spring Data JPA 2.0.3
Now (Spring data JPA 2.0.5 and above), after fixing problem with named queries, native one are also affected:
Tuple length in this case with SqlResultSetMappings is 1, because it knows what we want to return (look at org.hibernate.loader.hql.QueryLoader#getResultList method where results is coming as a list) and it is not a tuple, then it is checking with aliases from query's select clause and got a mismatch.
If I could help you somewhere or if something is still not clear I'm ready to help you with investigations
Affects: 2.1 M2 (Lovelace), 2.0.5 (Kay SR5), 1.11.12 (Ingalls SR12), 2.0.7 (Kay SR7)
Reference URL: https://github.com/EugeneNik/spring-data-datajpa-1280-example
Referenced from: commits 6098d01, c9605c5, c751659
Backported to: 2.0.8 (Kay SR8), 1.11.13 (Ingalls SR13)
3 votes, 7 watchers
The text was updated successfully, but these errors were encountered: