Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA CriteriaBuilder value like column

I'm trying to create a query using CriteriaBuilder where I need to have a predicate where the value of the predicate is like the value in the database.

Basically, I need to be able to do the following: WHERE myTestValue LIKE columnValue

In native queries, it is an option to do that, but using the CriteriaBuilder or NamedQueries, it does not seem to work.

String myValue = "[email protected]";
cb.where(cb.like(myValue, root.get(Entity_.email));

Is there an option in JPA to do it like this? Or should I fall back to native queries?

EDIT I need to be able to check if a given value matches a wildcard entry in database. So the database has a record %@bar.com%, and I need to check if my given value [email protected] matches to that record.

like image 240
Erates Avatar asked Mar 09 '23 20:03

Erates


1 Answers

I think your params should be other way round:

cb.where(cb.like(root.get(Entity_.email),myValue);

Aditionally you may need to use add this to the second param:

cb.where(cb.like(root.get(Entity_.email),"%"+myValue+"%");
like image 167
Maciej Kowalski Avatar answered Mar 19 '23 21:03

Maciej Kowalski