postgresql 如何在spring数据jpa查询where子句中编写hstore列

jv4diomz  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(1)|浏览(109)

我在Spring Data JPA查询的where子句中编写“hstore”类型列时遇到了一个问题。

@Transactional(readOnly = true)
@Query(
value = "select ld from LocationData ld inner join fetch ld.assetData ad where ld.assetId in (:assetIds) and ld.orgId = :orgId and ld.eventTimestamp between  :startDate and :endDate and ad.source in (:source) and 
            ad.attr -> 'CorrelationId' is not null",

countQuery = "select count(ld) from LocationData ld inner join ld.assetData ad where ld.assetId in (:assetIds) and ld.orgId = :orgId and ld.eventTimestamp between  :startDate and :endDate and ad.source in (:source) and ad.attr -> 'CorrelationId' is not null""

)

Page<LocationData> findByAssetIdInAndOrgIdAndEventTimestampBetween(@Param("assetIds") List<Long> assetIds,
                                                                       @Param("orgId") Integer orgId,
                                                                       @Param("startDate") Long startEventTimestamp,
                                                                       @Param("endDate") Long endEventTimeStamp,
                                                                       @Param("source") List<String> source,
                                                                       Pageable pageable);

字符串
在上面的查询中,“attr”是一个hstore类型的列。我在attr列中有一个名为CorrelationId的键。我想选择那些CorrelationId键不为空的。我尝试在pgAdmin中使用“ad.attr -> 'CorrelationId' is not null”,它工作得很好,但在Spring Data jpa查询中,它不工作,并作为无效标识符抛出错误。
有人能帮我吗。

jgwigjjp

jgwigjjp1#

如果JPA不喜欢“->”操作符,您可以尝试函数式表单

fetchval(ad.attr,'CorrelationId') IS NOT NULL

字符串
或者是

defined(ad.attr,'CorrelationId')

相关问题