2

I have created a postgres composite type and function as follows

CREATE TYPE  test AS (a int, b timestamp, c int);

CREATE FUNCTION some_updates(t test[])
    begin
    // iterate t and perform operation
    end

select some_updates(CAST(array[(488,'2019-7-01',342)] AS test[])

The above function and select to call the function works properly.

In spring jpa I would want to call function and pass values for "test[]" as params from native query. However it results in error.

@Query(value="select * from some_updates(:myinput)")
Myclass getDetails(List<test> myinput);

I have created a pojo class test as follows

class test
{
    int a;
    timestamp b;
    int c;

    //getters and setters
}

How can i possibly pass values to the postgres function?

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
Manu K R
  • 33
  • 4

1 Answers1

0

Pass an array literal instead of an array constructor. The value can be passed as string literal, then it is coerced to the right type by Postgres:

SELECT some_updates('{"(488, 2019-07-01 00:00:00,342)"}');

Precondition: the function is not "overloaded" so that function type resolution is unambiguous with untyped input.

Related:

If you have fewer than 100 array elements, a VARIADIC function would help to simplify passing arguments:

CREATE FUNCTION some_updates(VARIADIC t test[]) ...

Then pass row literals like:

SELECT some_updates('(488, 2019-07-01 00:00:00,342)'
                  , '(489, 2019-07-02 00:00:00,343)')

See:

If you have (many) more rows to pass, consider writing them to a (temporary) table with a plain INSERT and processing them from there.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • The function works perfectly when called within postgres sql. But I want to dynamically provide the parameters from java.Passing the request as string, i should manually code to convert the input to the above structure – Manu K R Jul 02 '19 at 08:58
  • @ManuKR: Not sure I understand the comment. – Erwin Brandstetter Jul 02 '19 at 10:07