我正在尝试使这个查询在 JPA 中工作:
SELECT * FROM contrat WHERE contrat_json @> '{"nom" :"hever"}';
它与 postgresql
完美配合,但是当我将它与 JPA 集成时,出现以下错误:
Parameter with that position [1] did not exist
我的代码:
@Transactional
@Query(nativeQuery = true,value = "select p from Contrat p where contrat_json @> '{\"nom\":\":nom\"}'")
public List<Contrat> findByNomRestrict(@Param("nom") String nom);
我认为它无法识别 @>
尽管进行了 native 查询,您有什么想法吗?
最佳答案
参数持有者在文字中不被理解:'...:nom...'
将包含字符 :nom
,而不是 的绑定(bind)值名称
。
对于 PostgreSQL 9.5(及更高版本),使用:
SELECT * FROM contrat WHERE contrat_json @> jsonb_build_object('nom', :nom)
对于 9.4:
SELECT * FROM contrat WHERE contrat_json @> CAST(json_build_object('nom', :nom) AS jsonb)
对于 9.3(及更早版本),没有 JSON 包含运算符(jsonb
类型也没有)。
关于java - JPA 和 JSON 运算符 native 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42901240/