用于搜索单词/字符串是否包含在实体字段之一中的JPQL查询 [英] JPQL query for searching if a word/string is consisted in one of entity's fields

查看:124
本文介绍了用于搜索单词/字符串是否包含在实体字段之一中的JPQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,它类似于查看句子中是否存在某个单词。
有实体帖子:

Basicaly, its similar like looking if certain word exists in sentence. There is entity Post:

public class Post implements Serializable {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "post_id", unique = true, nullable = false)
    private Integer id; 

    @Column(name = "post_title", length=300, unique = false, nullable = false)
    private String title;

    @Column(name = "post_date", unique = false, nullable = false)
    private Date date;
        ...}

我正在尝试实现 JPQL 查询将搜索发布具有特定字/字符串的实体实例( byTitle ,已通过作为参数)在 title 字段中,以及另外两个 Date 类型的参数,表示日期范围 - startDate endDate

I am trying to implement JPQL query that will search for Post entity instances that have certain word/string ( byTitle, passed as an argument) in their title field, and two more arguments of Date type, that represents date range - startDate and endDate.

我的上面有这样的东西mind:
SELECT p FROM Post p WHERE:byTitle IN(通过解析p.title字段创建的字符串集)AND p.date> =:startDate AND p.date< = endDate

I have something like this on my mind: SELECT p FROM Post p WHERE :byTitle IN (set of strings created from parsing p.title field) AND p.date>=:startDate AND p.date<=endDate

如何实现这种JPQL查询?或者也许应该使用JPA Criteria API?

How to implement this kind of JPQL query? Or maybe JPA Criteria API should be used?

编辑:
就像有 someString.contains( someSubstring) Java中的函数,JPQL中有类似的东西吗?或Criteria API?为了进行比较,它不必区分大小写。

Like there is someString.contains(someSubstring) function in Java, is there anything similar in JPQL? Or Criteria API? And for comparing, it doesn't have to be case sensitive.

推荐答案

我使用JPQL LIKE 与模式表达式配对:

I have implemented it using JPQL LIKE paired with pattern expression:

SELECT p FROM Post p WHERE 
       p.title LIKE :pattern AND 
               p.date>=:startDate AND 
                     p.date<=:endDate

其中 pattern =%+ someString +%

这篇关于用于搜索单词/字符串是否包含在实体字段之一中的JPQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆