简体   繁体   English

Java Spring 不支持的 StringMatcher REGEX

[英]Java Spring Unsupported StringMatcher REGEX

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:尝试将 StringMatcher.REGEX 与 java spring 一起使用时出现问题。没有编译器错误或任何错误,但是当您尝试使用上述字符串匹配器调用该项目时,您会收到以下错误:

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.请注意,上面的代码编译正常,执行时发生错误。

First, StringMatcher.REGEX is an entirely valid value.首先,StringMatcher.REGEX 是一个完全有效的值。 As you can see inside the ExampleMatcher.class file, you have 6 valid StringMatcher options:正如您在 ExampleMatcher.class 文件中所见,您有 6 个有效的 StringMatcher 选项:

    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:问题是正则表达式选项仅适用于 Java,不适用于 Java Spring。示例查询的 spring 文档指出:

Only supports starts/contains/ends/regex matching for strings and exact matching for other property types. 仅支持字符串的 starts/contains/ends/regex 匹配和其他属性类型的精确匹配。

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.根据 Spring 的jira 票证,不支持 StringMatcher.REGEX,而且永远不会,因为并非所有数据库系统都支持它。 Basically, you only have access to DEFAULT, EXACT, STARTING, ENDING, and CONTAINING string matcher items and REGEX will never work.基本上,您只能访问 DEFAULT、EXACT、STARTING、ENDING 和 CONTAINING 字符串匹配项,而 REGEX 将永远不起作用。 I hope this answer saves someone a lot of time.我希望这个答案可以节省很多时间。 I wasted too much trying to figure it out.我浪费了太多时间试图弄明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM