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

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

我在spring数据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键不为null的那些。我尝试使用“ad.attr - >'CorrelationId'不为空”在pgAdmin中它工作得很好但是在spring数据jpa查询中它不起作用并将错误作为无效标识符抛出。

谁可以帮我这个事。

postgresql postgresql-9.6 where hibernate hstore
1个回答
0
投票

如果JPA不喜欢“ - >”运算符,您可以尝试使用函数表单

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

要么

defined(ad.attr,'CorrelationId')
© www.soinside.com 2019 - 2024. All rights reserved.