简体   繁体   English

如何执行 postgresql json function 本机查询?

[英]How to execute a postgresql json function query in jpa nativequery?

Here i am trying to fetch an object named business from my jsonb column case_data where case_id is set through parameter.在这里,我试图从我的 jsonb 列 case_data 中获取一个名为 business 的 object ,其中 case_id 是通过参数设置的。

@Query(value="Select case_data->'business' from onboarding_cases where case_id=?1",nativeQuery=true)
 List<OnboardingCases> findByCaseAttrib(BigInteger caseId);

But it gives error: ERROR 26044 --- [nio-8091-exec-1] ohengine.jdbc.spi.SqlExceptionHelper: The column name case_id was not found in this ResultSet.但它给出错误:ERROR 26044 --- [nio-8091-exec-1] ohengine.jdbc.spi.SqlExceptionHelper:在此 ResultSet 中找不到列名 case_id。

Even though this column exists in the table.I tried using simple query but it also gives error.即使此列存在于表中。我尝试使用简单查询,但它也给出了错误。

   @Query("Select caseData->'business' from OnboardingCases where caseId=?1")
List<OnboardingCases> findByCaseAttrib(BigInteger caseId);

it gives error:Unexpected token at >它给出错误:>

The above query runs perfectly on pgAdmin4 but not in jpa.上面的查询在 pgAdmin4 上完美运行,但在 jpa 中却没有。

Finally did it.终于做到了。 Answering to help anyone seeking answer to this.回答以帮助任何寻求答案的人。 Changed the return type to List<String> .将返回类型更改为List<String>

@Query(value = "Select case_data->'business' from onboarding.onboarding_cases where case_id=?1", nativeQuery = true)
List<String> findByCaseAttrib(BigInteger case_id);

Had to add a special dependency so that jsonb data type can be mapped to hibernate.必须添加一个特殊的依赖项,以便 jsonb 数据类型可以映射到 hibernate。 Since hibernate has no such datatype by default.由于 hibernate 默认没有这样的数据类型。

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>2.5.0</version>
</dependency>

Special thanks to vladmihalcea for writing a jar for mapping jsonb datatypes in hibernate.特别感谢 vladmihalcea 编写了 jar 用于映射 hibernate 中的 jsonb 数据类型。 For more data see here .有关更多数据,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM