Support JPA 2.1 stored procedures returning result sets
  • 10-May-2023
Lightrun Team
Author Lightrun Team
Share
Support JPA 2.1 stored procedures returning result sets

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

Lightrun Team
Lightrun Team
10-May-2023

Explanation of the problem

The issue at hand pertains to the support for stored procedures (SPs) returning result sets in certain databases, such as MS SQL and MySQL. These databases allow SPs to return one or multiple result sets using simple SELECT statements. This pattern is commonly observed in MS Transact-SQL. The JPA 2.1 specification explicitly supports the mapping of SP result sets in the context of named stored procedure queries. It provides options to specify the mapping of result sets using either resultClasses or resultSetMappings annotations. When there are multiple result sets, the mappings should be specified in the same order as the result sets will be returned. If no explicit resultClasses or resultSetMappings are provided, the result sets are assumed to be returned as a list of type Object[]. However, Spring Data JPA does not seem to support this feature, as indicated in a related discussion on Stack Overflow.

To demonstrate the issue, an example test scenario is presented using pure JPA 2.1 executed within a Spring Boot 1.5.2 application. The example involves creating a stored procedure called “tmp_demo” that takes an integer argument. The procedure simply performs a SELECT operation on the argument, adding 1 to it, and returns the result as the “simple_result”. To handle this scenario, a custom repository named CustomDemoRepository is defined. It utilizes the EntityManager to create a StoredProcedureQuery object, registering the required parameter and setting its value. The getResultList method is called to obtain the result set from the stored procedure execution.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

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

When faced with the limitations of using stored procedures (SPs) with JPA and Spring Data JPA, there are a few workarounds that can be considered. One option is to utilize the Hibernate Session directly by using the createQuery or createNativeQuery methods and setParameterList to handle arrays. This approach allows for more flexibility in dealing with different SQL dialects and complex scenarios. However, it requires manual construction of SQL strings and may involve some challenges in terms of syntax and compatibility. Additionally, annotations like @Procedure and @NamedStoredProcedure might not be suitable for more advanced use cases involving arrays.

Another alternative is to leverage inline SQL or QueryDSL instead of relying solely on stored procedures. Inline SQL allows for more control and can simplify unit testing. QueryDSL, on the other hand, provides a type-safe way to build queries programmatically. While these approaches offer more flexibility and ease of use in certain scenarios, they may not be practical for handling complex stored procedures or views designed by data architects. There are valid reasons why companies choose to utilize procedures, views, and functions in their database architecture.

It is important to note that the limitation lies within JPA itself, and it may not be a straightforward task to address this issue. While developers and architects may find the lack of support for advanced stored procedure scenarios frustrating, it is crucial to explore available workarounds and consider alternative approaches to achieve the desired functionality. Spring Data JPA could potentially benefit from incorporating such features in the future to align with other solutions and address the challenges faced by developers who rely on stored procedures extensively.

Other popular problems with Spring Projects Spring Data JPA

Problem: Lazy loading related entities

Spring Data JPA provides the ability to retrieve related entities lazily by default. However, this can lead to LazyInitializationException when accessing related entities outside of the current transaction.

Solution:

To resolve this issue, you can fetch the related entities eagerly by using the FetchType.EAGER option in the @OneToOne, @OneToMany, or @ManyToMany annotations. For example:

@OneToOne(fetch = FetchType.EAGER)
private Address address;
EntityGraph graph = entityManager.createEntityGraph("graph.Person.address");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);

Person person = entityManager.find(Person.class, 1L, hints);

Problem: N+1 Select problem

The N+1 select problem occurs when fetching a collection of entities and loading the related entities one by one. This results in multiple database queries and can have a significant impact on performance.

Solution:

To resolve this issue, you can use the @EntityGraph or the join fetch clause in a JPQL query.

For example, using @EntityGraph:

EntityGraph graph = entityManager.createEntityGraph("graph.Person.address");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);

List<Person> people = entityManager
        .createQuery("SELECT p FROM Person p", Person.class)
        .setHint("javax.persistence.fetchgraph", graph)
        .getResultList();

For example, using join fetch in JPQL query:

List<Person> people = entityManager
        .createQuery("SELECT p FROM Person p JOIN FETCH p.address", Person.class)
        .getResultList();

Problem: Unidirectional Many-to-Many relationship mapping

A unidirectional many-to-many relationship occurs when one entity has a many-to-many relationship with another entity, but the relationship is not bi-directional. In this case, it is not possible to navigate from the related entity to the parent entity.

Solution:

To resolve this issue, you can make the relationship bi-directional by adding the appropriate mapping to the related entity. For example:

@ManyToMany(mappedBy = "people")
private List<Project> projects;

This will allow you to navigate from the related entity to the parent entity.

for (Project project : person.getProjects()) {
    System.out.println(project.getName());
}

A brief introduction to Spring Projects Spring Data JPA

Spring Data JPA is a library for simplifying the implementation of data access layers in Spring-based applications. It provides a convenient and efficient way to interact with databases using the Java Persistence API (JPA) and to implement common data access operations, such as CRUD (create, read, update, delete) operations.

Spring Data JPA extends the functionality provided by JPA and adds additional features, such as dynamically generated query methods, pagination and sorting, and the ability to interact with multiple databases using the same API. It also supports the use of different JPA implementations, such as Hibernate, EclipseLink, and OpenJPA. With the use of Spring Data JPA, developers can significantly reduce the amount of boilerplate code required to implement data access layers and focus on the application’s business logic.

Most popular use cases for Spring Projects Spring Data JPA

  1. Implementing data access layers for JPA-based applications:

Spring Data JPA can be used to simplify the implementation of data access layers in applications that use JPA. It provides a convenient and efficient way to interact with databases, perform common data access operations, and handle the underlying JPA implementation details.

For example, with Spring Data JPA, you can implement a simple data access layer using the following code:

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {

}
  1. Generating dynamic queries:

Spring Data JPA supports the generation of dynamic queries based on method names. This allows developers to write concise and expressive query methods without the need to write JPQL or SQL statements.

For example, you can retrieve all the Person entities with a specific firstName by defining the following method in the repository:

List<Person> findByFirstName(String firstName);
  1. Providing support for pagination and sorting:

Spring Data JPA supports pagination and sorting of query results. This allows developers to retrieve a subset of the results and present them in a specific order without the need to manually write the code to implement these features.

For example, you can retrieve a page of Person entities sorted by lastName by defining the following method in the repository:

Page<Person> findByAgeGreaterThan(int age, Pageable pageable);
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.