【发布时间】:2017-10-10 13:19:57
【问题描述】:
我有一张表casemessage 并有以下列。我正在尝试使用Spring Framework JPA 搜索/查询JSON 列..
- 身份证
- created_by
- created_utc
- from_id
- 留言
- 状态
- 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_id 是1 和status 不是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