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

@Procedure annotation doesn't work with cursors (NULL when using REF_CURSOR) and ResultSets that don't come from cursors [DATAJPA-1657] #1959

Closed
spring-projects-issues opened this issue Jan 7, 2020 · 3 comments
Assignees
Labels
in: core Issues in core support in: query-parser Everything related to parsing JPQL or SQL in: repository Repositories abstraction type: bug A general bug

Comments

@spring-projects-issues
Copy link

GabrielBB opened DATAJPA-1657 and commented

The solution provided in the ticket (https://jira.spring.io/browse/DATAJPA-1145) no longer works. The method with @Procedure returns null if you use a REF_CURSOR. Some co workers have tried with Oracle and Postgres, and I also found this StackOverFlow post from a year ago without no solution:

https://stackoverflow.com/questions/52787636/reading-ref-cursor-as-output-parameter-in-a-stored-procedure-with-spring-data-jp

I've checked other posts and people end up using the entity manager directly.

Additionally spring-data-jpa will never work without changes with procedures from MySQL and SQL Server databases. Those databases don't use cursors to return resultsets, so calling Hibernate getOutputParameterValue() won't work, you have to call getResultList(). Right now the people using spring-data-jpa with MySQL have to do this -> @Query("CALL My_Procedure()"), always after spending long time figuring out why @Procedure doesn't work (https://stackoverflow.com/questions/49274308/how-to-execute-a-stored-procedure-with-jpas-crudrepository-with-spring-bootpr/54112158#54112158)

Additionally I think the @Procedure API is missing returning ResultSets without using @NamedStoredProcedureQuery because you can already call procedures that return regular output type parameters like Integer or String without using named queries. But when it comes to REF_CURSOR and No REF_CURSOR resultsets you have to switch to named procedure queries, so the @Procedure API is not so consistent and it makes your entity feel dirty with all those tags after you had it clean using Lombok.

I made a PR to spring-data-jpa that fixes all of these issues. I've tested with 4 different databases

  • Make it possible to return ResultSets from @Procedure (not ref_cursor, MySQL, SQL Server)

  • Make it possible to return cursors from @Procedure (Oracle, Postgres)

  • Remove the need to use NamedStoredProcedureQuery in your entities to return cursors. We should be able to return it with a simple @Procedure annotation. No need to add long tags to our entities


Reference URL: https://stackoverflow.com/questions/52787636/reading-ref-cursor-as-output-parameter-in-a-stored-procedure-with-spring-data-jp

Referenced from: pull request #409

@spring-projects-issues
Copy link
Author

Jens Schauder commented

Sounds like a good improvement. I'll take a look at the PR

@spring-projects-issues
Copy link
Author

GabrielBB commented

Changed PR URL. With the new PR it will be easier to review the changes

@spring-projects-issues spring-projects-issues added type: bug A general bug in: core Issues in core support labels Dec 30, 2020
gregturn pushed a commit that referenced this issue Jul 9, 2021
When registered procedure output parameters, if the method annotated with @procedure has a collection return type or an entity return type, then use ResultSet. Otherwise, handle either the array (Object[]) or the primitive types.

* Throw a proper exception when using an @Procedure-annotated method and no transaction is active.
* Introduce refCursor as a boolean flag to @procedure denoting when to use REF_CURSORs for OUT parameters..
* Extract a ProcedureParameter command object to ease usage of a parameter's name, type and mode. (NOTE: javax.persistence already has "StoredProcedureParameter".)

NOTE: Integration testing of stored procedures is very limited by lack of HSQL's support for REF CURSORS. See ##2256 to track future work for potential testing against MySQL, Oracle, Postgres, or SQL Server.

See: #1959, #409.
Related: #2014.
gregturn added a commit that referenced this issue Jul 9, 2021
gregturn added a commit that referenced this issue Jul 9, 2021
@gregturn gregturn self-assigned this Jul 9, 2021
@gregturn gregturn added in: query-parser Everything related to parsing JPQL or SQL in: repository Repositories abstraction labels Jul 9, 2021
@gregturn gregturn added this to the 2.6 M1 (2021.1.0) milestone Jul 9, 2021
@gregturn
Copy link
Contributor

gregturn commented Jul 9, 2021

Resolved via 554bd3d.

Thanks @GabrielBB!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core support in: query-parser Everything related to parsing JPQL or SQL in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants