2

I'm now using spring data jpa to connect MySQL Server, how to use @Query to set regex search?

@Query(value = "select t from Tenants t where t.id regexp ?1")
Page regexSearch(String text, Pageable pageable);

My code not work, the error is "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: regexp near line 1, column 72 [select t from com.sg.beans.Tenants t where t.id regexp ?1]", does anyone know how to write for regex search?

1 Answer 1

5

JPQL does not support the regex function. You need to fall back to native SQL (using the nativeQuery flag on the @Query annotation).

Sign up to request clarification or add additional context in comments.

5 Comments

I don't find the @NativeQuery Class, Could you tell in which spring package?
There's no @NativeQuery class. As indicated in my answer, set the boolean flag nativeQuery in the @Query annotation to true to be able to use native SQL in your query declaration.
That's got nothing to do with Spring. We forward the declared query as is. If you gained further insights, saw other exceptions it's helpful to add them to the question. "Doesn't work" doesn't really help investigating the problem :).
So, why the exception is origniated by Spring ? How to get to the root of this exception?org.springframework.dao.InvalidDataAccessApiUsageException: Unsupported keyword REGEX; nested exception is java.lang.IllegalArgumentException: Unsupported keyword REGEX.
@jvmvik - Read up on that in the reference documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.