Spring Data Repository方法中如何使用Regex关键字 [英] How to use Regex keyword in Spring Data Repository Method

查看:92
本文介绍了Spring Data Repository方法中如何使用Regex关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用spring-data-jpa版本1.9.4.

我有一个MySql表,其中包含列project(整数),summary(varchar)和description(varchar).

I have a MySql table with columns project(integer), summary(varchar), and description(varchar).

我有一个正则表达式,我想用它来搜索摘要和/或描述字段,这意味着如果在摘要中找到它,则不需要将正则表达式应用于描述.

I have a regex that I would like to use to search the summary and/or description field meaning that if it finds it in summary does not need to apply regex to description.

我尝试使用的存储库方法是:

The repository method I am attempting to use is:

List<Issue> findByProjectAndSummaryOrDescriptionRegex(long project, String regex)

我收到的错误是:

java.lang.IllegalArgumentException:不支持的关键字REGEX(1): [MatchesRegex,Matches,Regex]

java.lang.IllegalArgumentException: Unsupported keyword REGEX (1): [MatchesRegex, Matches, Regex]

在公司环境中很难更新/升级版本,因此,如果问题不是我的语法,而是如果有人知道哪个版本现在支持"Regex"进行查询派生,或者在哪里可以找到该特定信息,那我就很难了.将感激不尽.我看过Changelog,似乎1.9.4应该支持,但似乎不支持.

It is difficult in my company environment to update/upgrade versions, so if the issue is NOT my syntax but rather the then if someone knows which version now supports 'Regex' for query derivation or where I could find that specific information I would be grateful. I have looked at the Changelog and it appears that 1.9.4 should support but it appears not.

感谢您的帮助!

JD

我知道@Query批注,但是如果我找不到支持关键字REGEX的正确版本[MatchesRegex,Matches,Regex],我的领导问他只能将其用作最后的手段

EDIT 1: I am aware of the @Query annotation but have been asked by my lead to only use that as a last resort if I cannot find the correct version which supports keyword REGEX [MatchesRegex, Matches, Regex]

推荐答案

如果Spring数据语法不起作用,我建议使用本机查询(带有@Query批注),例如:

I would recommend using native query (with @Query annotation) if the Spring data syntax does not work, e.g.:

@Query(nativeQuery=true, value="SELECT * FROM table WHERE project = ?1 AND (summary regexp ?2 OR description regexp ?2)")
List<Issue> findByProjectAndSummaryOrDescription(long project, String regex);

更新

如果不能使用本机查询,则(a)您是否可以在单列中尝试使用它,看看是否可行,以及(b)您可以通过将regex附加到两列中来进行尝试,例如:

If native query is not an option then (a) could you try it with single column and see if that works and (b) could you try by appending regex to both the columns, e.g.:

List<Issue> findByProjectAndDescriptionRegex(long project, String regex);

List<Issue> findByProjectAndSummaryRegexOrDescriptionRegex(long project, String regex, String regex);

这篇关于Spring Data Repository方法中如何使用Regex关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆