JPA和JSON运算符本机查询 [英] JPA and JSON operator native query

查看:729
本文介绍了JPA和JSON运算符本机查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使此查询在JPA中工作:

I'm trying to make this query work in JPA:

SELECT * FROM contrat WHERE contrat_json @> '{"nom" :"hever"}';

它与 postgresql 完美配合,但是当我将它与JPA集成,我得到以下错误:

It works perfectly with postgresql but when I integrate it with JPA, I get the following error:


该位置[1]的参数不存在

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);

我认为它无法识别 @> 尽管有本机查询,你有什么想法吗?

I think it does not recognize @> despite native query, do you have an idea?

推荐答案

使用PostgreSQL和JSON你可能会遇到需要或其他奇怪的运算符,所以最好只使用它们的函数等价物。您可以在 psql 控制台中查找它们,例如 \ doS + @>

With PostgreSQL and JSON you'll probably run into needing ? or other strange operators, so it's better you just use their function equivalents, instead. You can look them up in the psql console like this \doS+ @>.

您的查询不是原生的,如参数所示。

Your query is not native, as the parameter says.

select p from Contrat p where...

只有在到达数据库时才会出错。

Will only give you an error when it reaches the database.

尝试类似

@Query(nativeQuery = true, value = "select * from Contrat where jsonb_contains(contrat_json, :nom )")

只需绑定{ \nom \:\+ param +\}作为参数

这篇关于JPA和JSON运算符本机查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆