hibernate 错误地转换了对 jsonb 字段的查询
hibernate wrongly converts query for jsonb field
我有一个 jsonb 字段,我在其中存储了数据库中的键值映射。
不是我想在给定文本的地方进行查询,我想知道是否存在与文本类似的键。
例如,db 中的键是 'abc_def',我有文本 'abc',所以查询应该 return 记录。
我已经为它写了一个查询:
String sql = select * from table where (attrs->>'data')::jsonb::text like :queryKeyEspCorpCompany and status = 'ENABLED'
queryKeyEspCorpCompany 传递了一个参数。
但是当休眠实际上 运行 查询它 return 就像这样(在日志中)和 return 错误:
select * from table where '(attrs->>'data')':jsonb:text like ? and status = 'ENABLED'
错误是 jsonb 之前的“::”被转换为“:”。
有人可以帮助解释为什么会这样吗?
需要转义的字符:
:
String sql = "select * from table where (attrs->>'data')\:\:jsonb\:\:text like ?1 and status = 'ENABLED'";
因为它是本机查询,所以您必须使用 Postgres 参数放置语法:?1
、?2
、...
示例:
session.createSQLQuery(sql).setParameter(1, "abc_def").getResultList();
我有一个 jsonb 字段,我在其中存储了数据库中的键值映射。 不是我想在给定文本的地方进行查询,我想知道是否存在与文本类似的键。 例如,db 中的键是 'abc_def',我有文本 'abc',所以查询应该 return 记录。
我已经为它写了一个查询:
String sql = select * from table where (attrs->>'data')::jsonb::text like :queryKeyEspCorpCompany and status = 'ENABLED'
queryKeyEspCorpCompany 传递了一个参数。 但是当休眠实际上 运行 查询它 return 就像这样(在日志中)和 return 错误:
select * from table where '(attrs->>'data')':jsonb:text like ? and status = 'ENABLED'
错误是 jsonb 之前的“::”被转换为“:”。
有人可以帮助解释为什么会这样吗?
需要转义的字符:
:
String sql = "select * from table where (attrs->>'data')\:\:jsonb\:\:text like ?1 and status = 'ENABLED'";
因为它是本机查询,所以您必须使用 Postgres 参数放置语法:?1
、?2
、...
示例:
session.createSQLQuery(sql).setParameter(1, "abc_def").getResultList();