【问题标题】:JPA Query Pattern MatchingJPA 查询模式匹配
【发布时间】:2015-01-30 07:12:09
【问题描述】:

我有一个 JPA 存储库方法和相应的查询

@Query(select sc from ScheduleEntry sc where sc.doo ~'[?1]')

调度 findSchedule(String doo);

Doo 是由以数字表示的星期几组成的运营天数。 例如:如果营业日是周一、周三和周五,则 doo=135

现在我需要一个查询来查找在 doo 中包含任何给定日期的记录。 此查询在 postgresql 中有效,而 sc.doo LIKE '%[?1]%' 在 sql 中有效。 但是,这些都不是有效的 JPA 查询。请帮忙

【问题讨论】:

  • 第一个是完全无效的JPQL。 JPQL 中没有“~”运算符。这不是 SQL。阅读 JPQL 参考以查看可接受的语法

标签: hibernate postgresql jpa


【解决方案1】:

您可以在 JPQL 中尝试类似的操作。

sc.doo LIKE '%3%' //-- this is true for values - 135, 345, 123 etc. anywhere in the string

您可以创建查询并传递参数。

Query query = entityManager.createQuery("SELECT sc FROM ScheduleEntry sc WHERE sc.doo LIKE :dooValue");

query.setParameter("dooValue", "%"+3+"%");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多