如何在Spring Data JPA查询的where子句中编写hstore列

时间:2019-03-08 18:56:39

标签: postgresql postgresql-9.6 where hibernate hstore

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

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

如果JPA不喜欢“->”运算符,则可以尝试功能形式

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

defined(ad.attr,'CorrelationId')