Java Spring Unsupported StringMatcher REGEX

1.1k views Asked by At

There is an issue when attempting to use StringMatcher.REGEX with java spring. There is no compiler error or anything, but when you attempt to call the item using the above string matcher, you receive the following error:

2020-10-09 15:07:30.543 ERROR 29584 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Unsupported StringMatcher REGEX; nested exception is java.lang.IllegalArgumentException: Unsupported StringMatcher REGEX] with root cause

java.lang.IllegalArgumentException: Unsupported StringMatcher REGEX
    at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:210) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicate(QueryByExamplePredicateBuilder.java:102) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository$ExampleSpecification.toPredicate(SimpleJpaRepository.java:886) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:762) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]

The error can be seen when executing something along the lines of:

ExampleMatcher matcher = ExampleMatcher
                                .matchingAll()
                                .withStringMatcher(
                                        ExampleMatcher.StringMatcher.REGEX
                                 );

Note that the above code compiles just fine, the error happens when it is executed.

1

There are 1 answers

0
Nox On BEST ANSWER

First, StringMatcher.REGEX is an entirely valid value. As you can see inside the ExampleMatcher.class file, you have 6 valid StringMatcher options:

    public static enum StringMatcher {
        DEFAULT,
        EXACT,
        STARTING,
        ENDING,
        CONTAINING,
        REGEX;

        private StringMatcher() {
        }
    }

Because it is a valid entry, the code will compile just fine and see no issue. The problem is that the regex option only works in Java, not Java Spring. The spring documentation for query by example states:

Only supports starts/contains/ends/regex matching for strings and exact matching for other property types.

However, this is not the case. According to a jira ticket for Spring, there is no support for StringMatcher.REGEX and there never will be, because not all database systems support it. Basically, you only have access to DEFAULT, EXACT, STARTING, ENDING, and CONTAINING string matcher items and REGEX will never work. I hope this answer saves someone a lot of time. I wasted too much trying to figure it out.