Quick revision
The short answer
In Spring Data JPA we declare @Procedure(procedureName = "calculate_discount") BigDecimal calculateDiscount(Long customerId) on a repository, and the single OUT parameter becomes the return value. In plain JPA we create a StoredProcedureQuery with em.createStoredProcedureQuery("calculate_discount"), register the IN and OUT parameters, set the input, execute, and read getOutputParameterValue. @NamedStoredProcedureQuery defines the parameters once for reuse, result sets need REF_CURSOR or result class mappings, and on PostgreSQL we must know whether the routine is a function, called with SELECT, or a procedure, called with CALL.
What to remember
In this answer, we called calculate_discount in three ways: with Spring Data's @Procedure, where the single OUT parameter becomes the return value; with a StoredProcedureQuery where we register IN and OUT parameters, execute and read getOutputParameterValue; and through a reusable @NamedStoredProcedureQuery. We saw that result sets need REF_CURSOR or result class mappings, and that in PostgreSQL functions are selected while procedures are called with CALL, which affects the driver settings.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Spring Data JPA we declare @Procedure(procedureName = "calculate_discount") BigDecimal calculateDiscount(Long customerId) on a repository, and the single OUT parameter becomes the return value. In plain JPA we create a StoredProcedureQuery with em.createStoredProcedureQuery("calculate_discount"), register the IN and OUT parameters, set the input, execute, and read getOutputParameterValue. @NamedStoredProcedureQuery defines the parameters once for reuse, result sets need REF_CURSOR or result class mappings, and on PostgreSQL we must know whether the routine is a function, called with SELECT, or a procedure, called with CALL.
Background to this question
Stored procedures hold business logic in many older systems, and Java services still need to call them. JPA has had a standard API for this since version 2.1, and Spring Data wraps it in an annotation, but the details trip people up: parameter registration, OUT parameters, result sets and the PostgreSQL difference between functions and procedures. In this answer, we will recap what the procedure looks like, call it from Spring Data and from plain JPA, define it once with a named stored procedure query, handle procedures that return result sets, and finish with the PostgreSQL specifics.
1A Quick Recap of the Procedure
A stored procedure is a named routine stored in the database that takes parameters and runs SQL and procedural logic. IN parameters pass values in; OUT parameters pass values back to the caller. Ours takes a customer id and returns a discount.
In PostgreSQL, a caller of a procedure passes something, here null, in the position of each OUT parameter. JDBC hides that detail when we register the OUT parameter properly.
2Calling It From Spring Data JPA
Spring Data's @Procedure annotation maps a repository method to a stored procedure. The method parameters become IN parameters in order, and when the procedure has a single OUT parameter, its value becomes the method's return value.
If the procedure has several OUT parameters, the method can return a Map<String, Object> keyed by parameter name, check your Spring Data version for exactly how names are resolved. Calls run inside the current transaction like any other query, so the service usually marks the calling method @Transactional.
3Calling It From Plain JPA
Without Spring Data, we use StoredProcedureQuery from the EntityManager. We register each parameter with its position or name, its Java type and its mode, set the inputs, execute, and read the outputs.
6 messages in order. Play them, or step through them one at a time.
Notice we used positions rather than names. Named parameters in calls depend on driver support, and positional registration works across drivers, so it is the safer default. The registration order must match the order of parameters in the procedure's signature.
4Defining It Once With @NamedStoredProcedureQuery
If several places call the same procedure, repeating parameter registration is error-prone. @NamedStoredProcedureQuery on an entity declares the procedure, its parameters and their types once, under a name.
5Procedures That Return Result Sets
Some procedures return rows instead of a single value. Oracle procedures typically return them through an OUT parameter of type SYS_REFCURSOR, which in JPA we register with ParameterMode.REF_CURSOR. PostgreSQL functions can also return a refcursor, and Hibernate supports that pattern too, check your version. Other databases, such as SQL Server and MySQL, return result sets directly from the procedure.
In each case, we tell JPA what to map the rows to, either with a result class or a result set mapping, and read them with getResultList.
6PostgreSQL: Functions Versus Procedures
PostgreSQL has two kinds of routines. Functions, created with create function, return a value and are called in a query: select calculate_discount(42). Procedures, created with create procedure since PostgreSQL 11, are called with call and may manage transactions. Before version 11 everything was a function, so many older systems call functions where other databases would use procedures.
This matters for JDBC. The PostgreSQL driver has historically translated the JDBC call escape into a select, which works for functions. For real procedures, the driver's escapeSyntaxCallMode setting, with values such as callIfNoReturn or call, makes it send call instead; check your driver version. If the routine is a simple function, it is often easier to call it with a native query and skip the stored procedure API entirely.
7Follow-up questions
Why does the call fail with an error about the number of parameters?
Usually because the registered parameters do not match the routine's signature in count, order or type, or because the routine is a function and the driver is sending call, or the reverse. Checking the signature in the catalog and the driver's call mode fixes most of these.
Can a procedure call take part in the Spring transaction?
Yes. It runs on the transaction's connection like any statement, so its changes commit or roll back with the rest, as long as the procedure itself does not issue its own commit. A procedure that commits internally breaks the caller's transaction boundary.
Should new code put logic in stored procedures?
Only where it pays off, such as set-based data-heavy work close to the data. Procedures tie the code to one engine and need database skills to maintain, so new branching business rules usually belong in Java with tests.
8Conclusion
In this answer, we called calculate_discount in three ways: with Spring Data's @Procedure, where the single OUT parameter becomes the return value; with a StoredProcedureQuery where we register IN and OUT parameters, execute and read getOutputParameterValue; and through a reusable @NamedStoredProcedureQuery. We saw that result sets need REF_CURSOR or result class mappings, and that in PostgreSQL functions are selected while procedures are called with CALL, which affects the driver settings.
Open with
With Spring Data JPA I put a method on the repository annotated with @Procedure(procedureName = "calculate_discount"), taking Long customerId and returning BigDecimal. The method parameters are passed as IN parameters in order, and the single OUT parameter becomes the return value.
Then, if they want more
With plain JPA I call em.createStoredProcedureQuery("calculate_discount"), register parameter 1 as Long IN and parameter 2 as BigDecimal OUT, set parameter 1, call execute, and read getOutputParameterValue(2). I prefer positional registration because named parameters depend on the driver.
If several places call it, I would declare it once with @NamedStoredProcedureQuery on an entity, with the parameter modes and types, and refer to it by name from @Procedure or createNamedStoredProcedureQuery. For procedures that return rows, I register a REF_CURSOR parameter, which is the Oracle pattern, or pass a result class so Hibernate maps the rows.
On PostgreSQL I would check whether it is a function or a procedure: functions are called with select, procedures with call, and the JDBC driver needs escapeSyntaxCallMode set to send call. For a simple function, a native select query is often the easiest option.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 11, PostgreSQL 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show how to call a database stored procedure calculate_discount(IN customer_id BIGINT...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/720935c6c3/