【问题标题】:Postgres JSONB Check if JSON contains string using JPAPostgres JSONB 使用 JPA 检查 JSON 是否包含字符串
【发布时间】:2021-04-18 13:22:51
【问题描述】:

我的 posgtgres 表中有一个 jsonb 列,该 json 的结构为:{'alias':["name1","name2","name"....]}

我已经编写了 Postgres 查询来检查 JSON 对象中的数组是否包含名称。

select * from public.table t where json_col->'alias' ? 'name'

这适用于 pgAdmin

但在 JPA 中同样不起作用

我的代码

@Query(value = "select * from public.table t where json_col->'alias' ? :name" ,nativeQuery = true)
    Table findUsingName(@Param("name") String name);

这会引发错误:'Mixing of ?不支持?1等参数和其他形式!'

我知道这个错误是由于事实造成的?也被 JPA 使用是不同的意义...

任何人都可以在 JPA 查询中帮助我

【问题讨论】:

  • 改用??。我认为 JDBC 驱动程序会检测到这是一个“转义”? 运算符。

标签: postgresql hibernate jpa jsonb


【解决方案1】:

我不知道?运算符是做什么的,但是你应该使用运算符的命名变体,因为JDBC中的?是用来定义参数的。

【讨论】:

    【解决方案2】:

    找到 '?' 的 postgres 函数即 jsonb_exists_any(company_alias->'alias',ARRAY[:name])

    【讨论】:

    • 使用该函数的缺点是,如果出于性能原因需要,它不能使用索引。
    • 我试过了??但这是错误:'混合?参数和其他形式,如?不支持1!我别无选择
    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多