有人能告诉我如何使用 EclipseLink (jpa 2.1)
将数组参数传递给 postgresql 函数吗?
我尝试了以下但出现错误:
异常
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;
谢谢
最佳答案
我已经解决了这个问题。
我需要将二维字符串数组 (String[][]) 作为参数传递给 PostgreSQL 函数。
我所做的是将完整数组转换为字符串,然后将完整字符串中的方括号替换为花括号,如下所示:
query.setParameter(22, Arrays.deepToString(detalles_pedido).replace('[', '{').replace(']', '}'));
然后在我的 PostgreSQL 函数中,我将接收到的参数类型更改为 TEXT,并且我能够在 PostgreSQL 函数中使用此指令 variableName::TEXT[ ][ ] 将文本转换为数组。
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;
这对我来说真的很管用。
关于java - 如何使用 jpa 将数组参数传递给 postgresql 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25659010/