【问题标题】:how to write hstore column in where clause of spring data jpa query如何在spring数据jpa查询的where子句中编写hstore列
【发布时间】:2019-03-08 18:56:39
【问题描述】:

在 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 查询中它不工作并抛出错误作为无效标识符。

谁能帮帮我。

【问题讨论】:

  • FIRST - 忘记 Spring、Hibernate、Java 或 (insert_your_cool_development_environment_here)。使用 psql CLI(或您选择的 GUI 工具)运行原始查询 - 然后将其放入您的 IDE/框架中!如果查询在 psql 中有效,但在 spring 中无效,那么这是一个 IDE 问题。如果没有,将查询带回 dba.stackexchange,去掉 Java 代码!在添加新信息之前投票结束!

标签: postgresql postgresql-9.6 where hibernate hstore


【解决方案1】:

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

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

defined(ad.attr,'CorrelationId')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 2017-01-22
    • 2019-05-07
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多