使用Spring JPA @Query注释查询Postgres JSONB

4

我将创建一个Hibernate userTypeData,并成功地将我的对象(称为“data”)映射到Postgres表中的jsonb数据类型。现在,我正在尝试使用JpaRepository查询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列吗?


如果您有一个Spring Data方法参数,应该在查询字符串中使用参数占位符(:temp?)。 - pozs
2个回答

4

答案:对于Postgres的jsonb列,查询语法可以如下所示->

@Query(value = "SELECT * FROM genie_entities WHERE data ->> ?1 > ?2", nativeQuery = true)
public List<GenieEntity> findByDataFilterBigger(String key, String value );

它有效。:)

0
回答对我没有用,所以我写了下面的回答。我使用了@Param注解来代替数字,并通过它引用了这些值。
@Query(value = "SELECT * FROM genie_entities WHERE metadata ->> :key = :value", nativeQuery = true)  
Optional<GenieEntity> findByMetadataValue(@Param("key") String key, @Param("value") String value);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接