Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL query for searching if a word/string is consisted in one of entity's fields

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;
        ...}

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.

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

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

EDIT: 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.

like image 678
Vladimir Avatar asked Jan 15 '14 12:01

Vladimir


People also ask

Can we use subquery in JPQL?

A subselect is a query embedded into another query. It's a powerful feature you probably know from SQL. Unfortunately, JPQL supports it only in the WHERE clause and not in the SELECT or FROM clause. Subqueries can return one or multiple records and can use the aliases defined in the outer query.

Which JPQL keyword is used to determine whether a value is an element of a collection in GPA?

This example shows how to use JPQL keyword MEMBER OF to determine whether a value is an element of a collection. The value and the collection members must have the same type.

What do you understand by JPQL query?

The Jakarta Persistence Query Language (JPQL; formerly Java Persistence Query Language) is a platform-independent object-oriented query language defined as part of the Jakarta Persistence (JPA; formerly Java Persistence API) specification. JPQL is used to make queries against entities stored in a relational database.

Which of the following methods is used to execute a select JPQL query?

Query createQuery(String name) - The createQuery() method of EntityManager interface is used to create an instance of Query interface for executing JPQL statement.


2 Answers

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

where pattern = "%" + someString + "%".

like image 192
Vladimir Avatar answered Sep 27 '22 23:09

Vladimir


I think you must use several LIKE clauses connected by OR:

WHERE (p.title LIKE :firstWord OR ... OR p.title LIKE :lastWord) AND p.date >= :startDate AND p.date <= :endDate

and then set parameters:

query.setParameter("firstWord", '%' + firstWord + '%');
...
query.setParameter("lastWord", '%' + lastWord + '%');

But the query may be slow. You can consider using a dedicated fulltext search tool like Hibernate Search.

like image 27
Ján Halaša Avatar answered Sep 27 '22 23:09

Ján Halaša