question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support JPA 2.1 stored procedures returning result sets [DATAJPA-1092]

See original GitHub issue

Dmitriy Korobskiy opened DATAJPA-1092 and commented

Some databases: notably MS SQL and MySQL (but not Oracle) support SPs returning result sets (one or multiple) via simple SELECTs. In MS Transact-SQL this seems to be a pervasive pattern. JPA 2.1 spec explicitly supports mapping of SP result set(s) e.g. in 3.10.17.1 Named Stored Procedure Queries:

A stored procedure may return more than one result set. As with native queries, the mapping of result sets can be specified either in terms of a resultClasses or as a resultSetMappings annotation element. If there are multiple result sets, it is assumed that they will be mapped using the same mechanism — e.g., all via a set of result class mappings or all via a set of result set mappings. The order of the specification of these mappings must be the same as the order in which the result sets will be returned by the stored procedure invocation. If the stored procedure returns one or more result sets and no resultClasses or resultSetMappings element has been specified, any result set will be returned as a list of type Object[]. The combining of different strategies for the mapping of stored procedure result sets is undefined.

This feature does not seem to be supported by Spring Data JPA: e.g. see https://stackoverflow.com/questions/31097667/illegalargumentexception-type-cannot-be-null. I spent some time trying to make it work with both 1.5.2 and the current 2.0 snapshot to no avail.

Here is a test example working via pure JPA 2.1 (executed in Spring Boot 1.5.2 app):

CREATE PROCEDURE tmp_demo (@arg INT)
AS
BEGIN
  SELECT @arg + 1 AS simple_result;
END;

CustomDemoRepository:

import DemoResult;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Data
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CustomDemoRepository {

  //region Injected beans (via a RequiredArgsConstructor)
  private final EntityManager em;
  //endregion

  public List<DemoResult> execStoredProdDirectly(Integer arg) {
    StoredProcedureQuery spq = em.createStoredProcedureQuery("tmp_demo", DemoResult.class);
    spq.registerStoredProcedureParameter("arg", Integer.class, ParameterMode.IN);
    spq.setParameter("arg", 42);
    return spq.getResultList();
  }
}

DemoResult:

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data // = @ToString @EqualsAndHashCode @Getter-s @Setter-s (non-final) @RequiredArgsConstructor
@NoArgsConstructor
public class DemoResult implements Serializable {

  @Id private Integer simpleResult;
}

My thoughts:

  1. I understand that Spring Data JPA tries to map an SP OUT parameter to a repository @Procedure method result. This is fine, but when such a method is expected to return a list (List<DomainObject>) it means that developers need to get a result set out of an SP. In MS SQL result sets can be returned implicitly without declaring any additional SP params.
  2. Multiple result sets are used more rarely and support for those could be done separately from a single result set.

Affects: 1.11.1 (Ingalls SR1)

Reference URL: https://stackoverflow.com/questions/31097667/illegalargumentexception-type-cannot-be-null

Issue Links:

  • DATAJPA-1555 InvalidDataAccessApiUsageException occurs when using @Procedure annotation (“is duplicated by”)

2 votes, 5 watchers

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
netdragonboberbcommented, Feb 12, 2021

For the thousands of others reading this that are up against the same brick wall, before jumping to another library: I had the most luck using a straight Hibernate Session access with createQuery/createNativeQuery and setParameterList which is NOT available with JPA. However, due to different SQL dialects (e.g. PLPGSQL vs HQL) it’s still tricky and you’ll get things like mismatch of array notation you have to work through (e.g. missing brackets for postgres on createNativeQuery). I’ll follow up with “workaround” code samples when I get it working then turn the code into Foos and Bars.

Annotations in repos like @Procedure and @NamedStoredProcedure won’t work too well for you for anything beyond basic. If you don’t have arrays, you can use a custom JPA method. However, if you have arrays that isn’t going to work for you unless you hack it in as a string and manually build the array list to feed into the driver basically building a full SQL string to call the stored proc, which is messy. It also won’t work with Hibernate sessions and createStoredProcedureQuery. Stackoverflow will mislead you in a lot of cases because they are talking about simpler cases than you may be facing if you already are reading this.

Hibernate sessions and createQuery/createNativeQuery makes the arrays work with the aforementioned setParameterList method, and some elbow grease.

Before you tear your hair out, you should consider inline SQL or QueryDSL as well. Makes unit testing easier too. However, there are reasons companies often use procedures,views, and functions. Also, I’d like to see someone take a large stored proc or view written by a data architect and re-write that in QueryDSL or even inline SQL. Good luck with that.

I think Spring really needs to make this happen to be in line with other solutions. By the time a developer realizes these limitations, they are already on track to miss deadlines, and architects’ estimates destroyed because no one ever expects this limitation to exist in such a widely used product. I figure this story has happened thousands of times.

Sadly, I think the limitation is in JPA, so it’s not as simple as it seems. Multiple layers will need to be adjusted.

0reactions
schaudercommented, Feb 25, 2021

Triage result: This would be a nice feature but won’t be implemented in the near future.

A workaround is available: Execute the query in a custom method using either an injected EntityManager or a JDBC Connection possibly wrapped in a JdbcTemplate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to call stored procedures in JPA - Part 2 - Thorben Janssen
Stored procedures with REF_CURSOR​​ If you want to call a stored procedure that returns the resultset of a query, you can use a...
Read more >
Using JPA with stored procedures that return multiple result sets
I am currently working with Hibernate 4.3.5 Final and JPA 2.1, SqlServer 2008. The saga of my current approach can be found here:...
Read more >
Stored Procedures - JPA Queries - DataNucleus
Simple execution, returning a result set. A common form of stored procedure will simply return a single result set. You execute such a...
Read more >
Stored Procedures and Spring Data JPA - adrian.work
An Approach For Procedures Returning Result Sets. Posted on November 17, 2018. Spring Data JPA is an amazing product. It has some quirks, ......
Read more >
A Guide to Stored Procedures with JPA - Baeldung
2. Project Setup · 2.1. Maven Setup · 2.2. Persistence Unit Definition · 2.3. Table Creation Script · 2.4. Stored Procedure Creation on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found