'How to call a stored procedure in Spring Boot?

A query for a stored procedure for multiple delete yield an error message shown way below.

CREATE OR REPLACE PROCEDURE delete_dim_page(_page_id bigint)
 LANGUAGE plpgsql
 AS $procedure$
    
begin
   delete from page_catalog where page_type_id = _page_id;
   delete from page_type where page_type_id = _page_id;
END;
$procedure$
;

//entityclass

@NamedStoredProcedureQuery(
    name = "firstProcedure",
    procedureName = "delete_dim_page",
    parameters = {
        @StoredProcedureParameter(
            mode=ParameterMode.IN,
            name="_page_id",
            type = Long.class
        )
    }
)

//service.java

    StoredProcedureQuery query = em.createNamedStoredProcedureQuery("firstProcedure");
    query.setParameter("_page_id", pageTypeId);
    query.execute();

yields

ERROR: delete_dim_page1(bigint) is a procedure
Hint: To call a procedure, use CALL.

What is a recommendable way to call a stored procedure in Spring Boot?



Solution 1:[1]

I suppose yiu are using spring data jpa. You can just create a jpa repository et call your procedure with a annontation like that :

public interface PageCatalogRepository extends JpaRepository<PageCatalogEntity , Long> {
    @Query(value = "CALL firstProcedure(:page_id);", nativeQuery = true)
    void deletePageCatalogById(@Param("page_id") Long page_id);
}

You can find other procedure calling method here https://www.baeldung.com/spring-data-jpa-stored-procedures

Solution 2:[2]

Another way is using annotation @Procedure

import org.springframework.data.jpa.repository.query.Procedure;
...
public interface PageCatalogRepository extends JpaRepository<PageCatalogEntity , Long> {
   @Procedure
   public String firstProcedure(Long page_id);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 soung
Solution 2 Johan