Saturday, 11 October 2014

Spring Data: How to call a stored procedure

Spring data reference supports calling stored procedure (from JPA 2.1) but didn't work for me. Therefore I called the stored procedure with a native query:

public interface MyEntityRepository extends PagingAndSortingRepository<MyEntity, Long> {
    @Query(nativeQuery=true, value="{call GetFoo()}")
    Object[] callMyStoredProcedure();


1) We are obliged to write the call in an entity repository. Therefore choose an entity in random (e.g. I used MyEntity) and place there your stored procedure call.

2) The stored procedure with name GetFoo() joins some tables, etc, and returns a row. Its columns are stored in the returned Object[].

PS: For Spring Data and Stored Procedures check the following sites:

No comments:

Post a Comment