【问题标题】:How to create a native query in JPA如何在 JPA 中创建本机查询
【发布时间】:2021-06-17 01:01:11
【问题描述】:

我有一个查询,检查列 jsonb 中的 List 是否存在如下字符串:

@Query(value = "SELECT item FROM catalog_cache WHERE tags ? 'somestring' ", nativeQuery = true)

JPA 中是否有针对 jsonb 操作的特殊语法,例如 '?'、'?|'、'@>'。

原因:org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"

但它不起作用。任何想法为什么?

【问题讨论】:

  • 查询的返回应该是什么?项目是列吗?
  • 它返回项目。但是对于 JPA 来说,这并不重要吗?是特殊符号,即使我将其设置为 nativeQuery。我需要 JPA 用 |?、&?、? 理解真正的原生 SQL JSONB 语法

标签: postgresql jpa jsonb


【解决方案1】:

更正你的语法,它会是这样的:

@Query(value = "SELECT * from catalog_cache WHERE where tags = ?1", nativeQuery = true)
List<CatalogCacheEntity> findWhatEverName(String value);

【讨论】:

  • 它不起作用,因为标签是 JSONB 列。引起:org.postgresql.util.PSQLException:错误:运算符不存在:jsonb = 字符变化
  • @user3693537 你必须更具体,但例如看看这里:stackoverflow.com/questions/43900457/…,有一个叫做jsonb_extract_path_text
  • 如果更具体:我有一个包含列 jsonb 标记的表。标签是列表结构。 [“x”、“y”、“z”]。我创建了索引 CREATE INDEX catalog_cache_tags_inx ON catalog_cache USING GIN ((tags));所以为了更有效地工作,我需要执行查询 SELECT * from catalog_cache WHERE where tags ? 'X'。查找列标签包含“x”的所有行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 2019-07-22
  • 2017-11-25
  • 1970-01-01
  • 2018-10-27
  • 2013-10-08
相关资源
最近更新 更多