我有这样的 Spring 数据存储库接口:
public interface MyEntityRepository extends
JpaRepository<MyEntity, Long> {
@Query(nativeQuery = true, value = "select * from my_func(:myList)")
Page<MyEntity> findBy(
@NonNull @Param("myList") List<String> myList,
@NonNull Pageable pageable);
}
Postgres 的函数我是这样定义的(但如果我做错了,我可以更改它):
CREATE OR REPLACE FUNCTION my_func(variadic myList text[])
RETURNS SETOF myEntityTable AS $$
... some logic
select * from myEntityTable t where t.foo in (myList);
当我调用此存储库方法时,出现此错误:
ERROR: operator does not exist: character varying = text[]
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Where: PL/pgSQL function f_najdi_autorizaciu_na_spracovanie(text[]) line 28 at RETURN QUERY
你能告诉我我的 postgres 函数应该使用什么类型吗?谢谢你的建议。
编辑:
我无法使用存储库方法之上的本机查询并将其列表传递到 IN 子句中,因为我在 DB 函数中有更多逻辑、变量等......它必须是 DB 函数。
我在类似情况下使用了以下解决方案:
1)创建两个辅助函数:
-- Convert a variable number of text arguments to text array
-- Used to convert Java collection to the text array
--
create or replace function list_to_array(variadic _list text[]) returns text[] language sql as $$
select _list;
$$;
-- Convert the bytea argument to null.
-- Used to convert Java null to PostgreSQL null
--
create or replace function list_to_array(_list bytea) returns text[] language sql as $$
select null::text[];
$$;
2) Used any
代替in
在主函数中,例如:
create or replace function my_func(_params text[])
returns table (field1 text, field2 text)
language sql as
$$
select
t.field1 as field1,
t.field2 as field2,
from
my_table t
where
array_length(_params, 1) is null or t.foo = any(_params);
$$;
3)然后在存储库方法中使用它们,例如:
@NonNull
@Query(value = "select ... from my_func(list_to_array(?1))", nativeQuery = true)
List<MyProjection> getFromMyFunc(@Nullable Set<String> params, @NonNull Pageable page);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)