1

could someone tell me how to pass an array parameter to a postgresql function using EclipseLink (jpa 2.1)? I tried the below but get error:

Exception

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of [Ljava.lang.Integer;. Use setObject() with an explicit Types value to specify the type to use.

Integer[] propertyRowIndexArr = new Integer[]{1005, 1006, 1007};
StoredProcedureQuery query1 = myEntityManager.createStoredProcedureQuery("func_testArr");
query1.registerStoredProcedureParameter("itemdetailid", Integer[].class, ParameterMode.IN);
query1.setParameter("itemdetailid", propertyRowIndexArr);


CREATE OR REPLACE FUNCTION func_testArr(categoryid integer[])
  RETURNS setof category AS
$BODY$ 

DECLARE r category%rowtype;

BEGIN
        FOR r IN select * from category c where c.categoryid = any($1)
        LOOP
            RETURN NEXT r; 
        END LOOP;

END;

Thanks

5
  • 2
    This and this might help you. Sep 4, 2014 at 7:13
  • thanks @ankur, the links were help full.
    – fs2050
    Sep 5, 2014 at 10:16
  • does this solves your problem, then i should post this as an answer for your acceptance Sep 5, 2014 at 10:17
  • yes it did. It gave me an idea. Instead of passing an array to the function, I passed a string and delimiter the integers with a comma. Then, I used string_to_array inside the function to split the string into an array. Thanks,
    – fs2050
    Sep 5, 2014 at 12:59
  • answer posted for your acceptance Sep 5, 2014 at 15:25

1 Answer 1

0

I did a workaround about this problem.

I needed to pass bidimensional string array (String[][]) as parameter into PostgreSQL function.

What I did was convert the full array to string, and then replace the square brackets by curly braces in the full string like this:

  query.setParameter(22, Arrays.deepToString(detalles_pedido).replace('[', '{').replace(']', '}')); 

Then in my PostgreSQL function I changed the parameter type received to TEXT and I was able to cast the text to array using this instruction variableName::TEXT[ ][ ] inside the PostgreSQL function.

validacion := 0;    
FOREACH detalle SLICE 1 IN ARRAY set_detalles::text[][] LOOP    
    oidDetalle = uuid_generate_v4();
    SELECT "Oid" INTO productoID FROM public.cat_producto WHERE  descripcion = detalle[1];
    SELECT precio_unitario INTO precioUnitario FROM public.cat_producto WHERE  descripcion = detalle[1];

    SELECT count(dblink) INTO validacion FROM (SELECT dblink('dblink_trans', 'INSERT INTO public.detalle_pedido ("Oid", pedido_id , producto_id, cantidad, surtido, precio, "OptimisticLockField", "GCRecord") VALUES (''' || oidDetalle || ''',''' || Oid || ''',''' || productoID || ''', '|| detalle[2] || ', false,'|| precioUnitario || ', 0, null);')::TEXT) AS insercion;                    
END LOOP;

This really worked good for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.