Home > database >  Array parameters to a postgresql function with SpringBoot/JpaRepository
Array parameters to a postgresql function with SpringBoot/JpaRepository

Time:02-04

I am trying to call a postgresql function from a JpaInterface, but I don't know why it can't work. I tried to make a very simple function to figure it out but couldn't find the answer. Can you please help me ? Thanks in advance.

public interface Interface extends JpaRepository<Model,Integer> {
    @Procedure("test_proc")
    int testProc(List<Integer> array);
}

Below is the sql function :

CREATE OR REPLACE FUNCTION public.test_proc(p_nbs integer[])
 RETURNS integer
 LANGUAGE plpgsql
AS $function$
declare
begin 
    return array_length(p_nbs, 1);
end;
$function$
;

I would like to know what to put instead of the List and how I can call testProc.

Thank you

CodePudding user response:

I found something for my problem but am not happy with the solution :

Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
    final CallableStatement callable = connection.prepareCall("select func_testArr(?)");
    final Integer[] strings = {1, 2, 3};
    final Array stringsArray = connection.createArrayOf("int", strings);
    callable.setArray(1, stringsArray);
    callable.execute();

});

I would prefer a way to use the annotation @Procedure with JPA.

  •  Tags:  
  • Related