Roland
Roland

Reputation: 7853

Compare Long to String

In JPQL in a where condition I want to do:

book.id like lower(:search)

The problem: book.id is a Long but the :search parameter is a String. Will the above just work the way it is or do I need to explicitly convert book.id to String and if that's the case, how do I do it?

Upvotes: 0

Views: 1289

Answers (2)

Julio Villane
Julio Villane

Reputation: 1034

You can use the CAST statement in JPQL like:

CAST(book.id AS string) LIKE lower(:search)

But with this solution you won't be able to use any index on the book.id column.

Upvotes: 1

wypieprz
wypieprz

Reputation: 8219

I would convert :search parameter to Long and change the query condition to:

book.id = :search

then

List<Books> books = em.createQuery("... WHERE book.id = :search")
                      .setParameter("search", Long.valueOf(strSearch))
                      .getResultList();

In general processing numbers is faster than strings.

Upvotes: 1

Related Questions