@Procedure is a Spring Data JPA annotation that maps a repository method to a database stored procedure. You declare the method on a repository interface, name the procedure, and Spring Data builds and executes the JPA StoredProcedureQuery for you, binding the method arguments as procedure parameters.
Use it when logic already lives in the database, or when a set-based operation is much faster inside the database than in Java. The annotation is org.springframework.data.jpa.repository.query.Procedure, and it works on repository interfaces that extend JpaRepository or CrudRepository.
A repository method annotated with @Procedure is executed as a stored procedure call instead of a derived or JPQL query. Spring Data resolves which procedure to call in this order:
procedureName (or its alias value) on the annotation.name that points to a @NamedStoredProcedureQuery declared on the entity.Entity.methodName, found on the entity of the repository.| Attribute | Purpose |
|---|---|
value / procedureName | Name of the procedure in the database |
name | Name of a @NamedStoredProcedureQuery on the entity |
outputParameterName | Which OUT parameter becomes the return value |
refCursor | Set to true when the procedure returns a REF_CURSOR (for example on Oracle or PostgreSQL) |
Suppose the database has this procedure (MySQL syntax):
The repository calls it by name. The procedure name goes in procedureName, not in a method-level name that refers to a named query:
The @Param values must match the parameter names declared in the procedure. If you leave @Param off, arguments are bound by position, in method-argument order, which is fine for simple procedures but fragile when the signature changes.
A procedure that modifies data needs a transaction. Call the repository from a @Transactional service method, or annotate the repository method with @Transactional.
When a procedure has one OUT parameter, a repository method with a non-void return type receives it.
Calling employeeRepository.countOlderThan(30) returns the value of total. Use outputParameterName when the procedure has several OUT parameters and you want one specific value; to read several at once, use an entity-level named query that returns a Map.
For multiple parameters of different modes, or a result set mapped to an entity, declare the procedure on the entity and reference it from the repository.
Here name refers to the named query, and the result set of the procedure is mapped to Employee entities. This is the usual way to read rows with a stored procedure.
CREATE PROCEDURE need a call with CALL, and older drivers or Hibernate versions may require a function with RETURNS instead. Check the Hibernate and driver versions you run.javax.persistence types are replaced by jakarta.persistence in Spring Boot 3 and later.A normal derived query, JPQL or @Query is easier to test, version and port between databases. Choose @Procedure when the procedure already exists and is owned by a database team, when it must run inside the database for performance (large batch updates), or when permission rules force access through procedures only.
@Query runs a JPQL or native SQL statement that you write in the repository. @Procedure invokes a stored procedure that already exists in the database and binds parameters to it.
Yes, through a @NamedStoredProcedureQuery with resultClasses, referenced by @Procedure(name = "..."). The procedure must return a result set the driver can read.
If the procedure changes data, yes. Run the call inside a transaction so the changes commit or roll back with the rest of your unit of work.
Usually the name does not match the database name, the schema is not on the connection's default search path, or you put the procedure name in name instead of procedureName.
@Procedure lets a Spring Data JPA repository call database stored procedures with plain Java method signatures. Set procedureName for direct calls, outputParameterName to read an OUT value, or name to reuse a @NamedStoredProcedureQuery that maps result sets to entities. Keep calls transactional, test against the real database, and use ordinary queries when you do not need a procedure.