Search code examples
expressionjpa-criteria

JPA Criteria Expression Replace Character


In type String to replace all - with * I can write

String firstName = firstName.replaceAll("-", "*");

Is there a way to do the same in Criteria Expression?

I need to remove special characters from the first name before comparing it to the pattern in like method.

My expression:

Expression<String> firstName = root.get("firstName");
builder.like(firstName, pattern);

Solution

  • Thanks to @SimonMartinelli comment. My issue is solved by using

    builder.function("REGEXP_REPLACE", String.class, firstName, builder.literal("[^a-zA-Z0-9]+"), builder.literal(""));