【问题标题】:Spring Boot JPA: how do query a JSON column in a tableSpring Boot JPA:如何查询表中的 JSON 列
【发布时间】:2017-10-10 13:19:57
【问题描述】:

我有一张表casemessage 并有以下列。我正在尝试使用Spring Framework JPA 搜索/查询JSON 列..

  1. 身份证
  2. created_by
  3. created_utc
  4. from_id
  5. 留言
  6. 状态
  7. case_id

这里的status 列存储JSON 字符串的列表。例如:

 1. [{"user_id": 1, "status": "sent"}, {"user_id": 2, "status": "delete"}]
 2. [{"user_id": 3, "status": "delete"}, {"user_id": 2, "status": "sent"},{"user_id": 1, "status": "received"}]
 3. [{"user_id": 1, "status": "received"}, {"user_id": 2, "status": "sent"}]
 4. [{"user_id": 1, "status": "delete"}, {"user_id": 3, "status": "sent"}]

我正在尝试查询casemessage 表以获取user_id1status 不是delete 的所有行

使用MySQL 查询,我能够查询表并返回预期结果。

这是我尝试过的查询:

 select * from casemessage  where case_Id=1 and id not in(select id from cwot.casemessage where json_contains(status, '{"status" :"delete"}') and json_contains(status, '{"user_id" : 1}'));

当我使用Spring Framework JPA (Spring Boot) 尝试此操作时,运行应用程序时出现异常。这是我绑定的声明:

    @Query("select c from CaseMessage c  where c.caseId=?1 and c.id not in(select cm.id from CaseMessage cm where json_contains(status, '{\"status\": \"delete\"}') and json_contains(status, '{\"user_id\": ?2}'))")
    List<CaseMessageResponse> getAllCaseMessages(long caseId, long userId);

我得到的错误是:

 Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 172 [select c from com.cwot.domain.CaseMessage c  where c.caseId=?1 and c.id not in(select cm.id from com.cwot.domain.CaseMessage cm where json_contains(status, '{"status": "delete"}') and json_contains(status, '{"user_id": ?1}'))]

有人可以帮我解决这个问题吗?

非常感谢任何帮助。

【问题讨论】:

  • @e4c5,我在看 JPA 的观点。我的查询在 MySQL 中工作
  • 这没有任何区别。这是不适合 JSON 的情况
  • 我有一个疑问。如果我需要检查是否有任何称为状态的键,那么如何检查呢?

标签: mysql json spring-boot spring-data-jpa


【解决方案1】:

您必须使用原生查询才能使用 json_contains 等数据库函数:

@Query("select c from CaseMessage c  where c.caseId=?1 and c.id not in(select cm.id from CaseMessage cm where json_contains(status, '{\"status\": \"delete\"}') and json_contains(status, '{\"user_id\": ?2}'))", nativeQuery = true)
    List<CaseMessageResponse> getAllCaseMessages(long caseId, long userId);

或使用@NativeQuery 注释

更多信息:

Difference between query, native query, named query and typed query

【讨论】:

  • 当我使用这个语句运行时,我得到一个错误,说org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [2] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [2] did not exist
  • 你必须修改你的查询,它必须成为一个原生的
  • 不想使用原生查询怎么办?有什么解决办法吗?
猜你喜欢
  • 2021-10-13
  • 1970-01-01
  • 2021-09-15
  • 2022-01-24
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2017-04-01
  • 2021-04-16
相关资源
最近更新 更多