Java Spring不支持的StringMatcher正则表达式

huangapple go评论76阅读模式
英文:

Java Spring Unsupported StringMatcher REGEX

问题

在尝试在Java Spring中使用StringMatcher.REGEX时存在问题。没有编译错误或其他问题,但是当尝试使用上述字符串匹配器调用该项时,会收到以下错误消息:

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 [] 引发异常 [请求处理失败嵌套异常为org.springframework.dao.InvalidDataAccessApiUsageException不支持的StringMatcher REGEX嵌套异常为java.lang.IllegalArgumentException不支持的StringMatcher REGEX] 根本原因是

java.lang.IllegalArgumentException不支持的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]

当执行类似以下内容时,可以看到该错误:

ExampleMatcher matcher = ExampleMatcher
                                .matchingAll()
                                .withStringMatcher(
                                        ExampleMatcher.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:

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

得分: 3

首先,StringMatcher.REGEX 是一个完全有效的值。正如你可以在 ExampleMatcher.class 文件内看到的那样,你有 6 个有效的 StringMatcher 选项:

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

        private StringMatcher() {
        }
    }

因为这是一个有效的选项,代码将会编译得很好,而且不会出现任何问题。
问题在于 regex 选项只在 Java 中有效,而不适用于 Java Spring。Spring 的查询按示例文档中指出:

> 仅支持对字符串进行 starts/contains/ends/regex 匹配,对于其他属性类型仅支持精确匹配。

然而,实际情况并非如此。根据 Spring 的一个 jira 票据,Spring 不支持 StringMatcher.REGEX,而且永远不会支持,因为并不是所有的数据库系统都支持它。基本上,你只能访问 DEFAULT、EXACT、STARTING、ENDING 和 CONTAINING 字符串匹配项,而 REGEX 将永远不会起作用。希望这个答案能为某人节省很多时间。我在试图弄清楚这一点时浪费了太多时间。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月10日 03:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定