如何在jpa nativequery中执行PostgreSQL json函数查询?

问题描述 投票:0回答:1

这里,我试图从我的jsonb列case_data中获取名为business的对象,其中case_id是通过参数设置的。

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

但是它给出了错误:错误26044-[nio-8091-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper:在此ResultSet中找不到列名case_id。

即使表中存在此列。我尝试使用简单查询,但也会给出错误。

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

它给出错误:>上的意外令牌

上面的查询可以在pgAdmin4上完美运行,但不能在jpa上运行。

java json postgresql jpa nativequery
1个回答
0
投票

最后完成了。正在帮助任何寻求此答案的人。将返回类型更改为List。

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

必须添加特殊的依赖关系,以便jsonb数据类型可以映射到hibernate。因为hibernate默认没有这种数据类型。

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

[特别感谢vladmihalcea编写了一个用于在休眠状态下映射jsonb数据类型的jar。https://github.com/vladmihalcea/hibernate-types有关更多数据,请参见上面的链接。

© www.soinside.com 2019 - 2024. All rights reserved.