I have the following JPQL query:
SELECT il
FROM InsiderList il
WHERE ( il.deleteFlag IS NULL OR il.deleteFlag = '0' )
AND il.clientId = :clientId
AND ( LOWER( il.name ) LIKE :searchTerm
OR il.nbr LIKE :searchTerm
OR LOWER( il.type ) LIKE :searchTerm
OR LOWER( il.description ) LIKE :searchTerm )
The customer wants us to be able to search be the nbr
field, which is a java.lang.Long
.
Q:
How do you perform a LIKE search on a java.lang.Long
using JPQL?
Query createQuery(String name) - The createQuery() method of EntityManager interface is used to create an instance of Query interface for executing JPQL statement.
As mentioned earlier, JPQL doesn't support LIMIT and OFFSET clauses. But you can use the same setFirstResult and setMaxResults methods of the Query and TypedQuery interface that I showed you in the previous section. . getResultList();
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.
You can simply use CAST(num as string)
or CONCAT(num,'')
. It worked for me
You can use the CAST
in HQL:
SELECT il
FROM InsiderList il
WHERE ( il.deleteFlag IS NULL OR il.deleteFlag = '0' )
AND il.clientId = :clientId
AND ( LOWER( il.name ) LIKE :searchTerm
OR CAST( il.nbr as string ) LIKE :searchTerm
OR LOWER( il.type ) LIKE :searchTerm
OR LOWER( il.description ) LIKE :searchTerm )
But you can have serious performance problems doing this, because the database will cannot use the nbr
index (if nbr
column is indexed).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With