使用spring jpa @query注释查询postgres jsonb
问题描述:
我创建了一个hibernate userTypeData,并成功将我的对象(称为“数据”)映射到postgres表中的jsonb数据类型。现在,即时尝试使用JpaRepository来查询该jsonb列,但没有成功。这是我使用的查询代码:使用spring jpa @query注释查询postgres jsonb
public interface GenieEntityDao extends JpaRepository<GenieEntity, Long>, QueryByExampleExecutor<GenieEntity> {
@Query(value = "SELECT e FROM from genie_entities e WHERE e.data ->> 'temp' = '30'", nativeQuery = true)
public List<GenieEntity> findByTempBiggerThan(String temp);
}
这是我得到的例外:
org.springframework.dao.InvalidDataAccessApiUsageException: Unknown parameter position: 1; nested exception is java.lang.IllegalArgumentException: Unknown parameter position: 1
有人知道如何使用@Query注释查询jsonb列?
答
答:Postgres的jsonb列的查询语法可以是这样的 - >
@Query(value = "SELECT * FROM genie_entities WHERE data ->> ?1 > ?2", nativeQuery = true)
public List<GenieEntity> findByDataFilterBigger(String key, String value);
它的工作原理。 :)
如果您有spring-data方法参数,则应在查询字符串(':temp'或'?')内使用参数占位符。 – pozs