JPQL (JPA) 搜索子字符串

发布于 2024-08-30 18:55:53 字数 239 浏览 6 评论 0原文

我面临着通过某些(子)字符串搜索实体的简单问题,它们可能包含这些字符串。

例如,我有用户 user1、usr2、useeeer3、user4,我将输入搜索窗口“use”,我希望返回 user1、useeer3、user4。

我相信你现在知道我的意思了。 JPA(JQPL)中是否有任何构造?在命名查询中以某种方式使用 WHERE 进行搜索会很好。类似“从用户 u 中选择 u,其中 u.nickname 包含 :substring”

Im facing simple problem with searching entities by some (sub)string, which they might contain.

E.g. I have users user1, usr2, useeeer3, user4 and I will enter to search window "use" and I expect to return user1, useeer3, user4.

Im sure you know what I mean now. Is there any construction in JPA (JQPL)? It would be nice to search using WHERE somehow in named queries. Something like "SELECT u FROM User u WHERE u.nickname contains :substring"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2024-09-06 18:55:53

使用 LIKE 表达式。以下是 JPA 1.0 规范 (JSR 220) 的 4.6.9 Like Expression 部分的引用:

使用的语法
比较运算符[NOT] LIKE
条件表达式如下:

字符串表达式 [NOT] LIKE 模式值 [ESCAPE 转义字符]

字符串表达式必须有一个
字符串值。 pattern_value 是一个
字符串文字或字符串值
输入参数,其中下划线
(_) 代表任意单个字符,a
百分号 (%) 字符代表任意
字符序列(包括
空序列),以及所有其他
角色代表他们自己。这
可选的 escape_character 是
单字符字符串文字或
字符值输入参数
(即 charCharacter)并且是
用于逃避特殊含义
下划线和百分号字符
pattern_value中。

示例是:

  • address.phone LIKE '12%3' 对于“123”、“12993”为 true,对于“1234”为 false
  • asentence.word LIKE 'l_se' 对于 'lose' 为 true,对于 'loose' 为 false
  • aword.underscored LIKE '\_%' ESCAPE '\' 对于 '_foo' 为 true 并且
    'bar' 为 false
  • address.phone对于“123”和“12993”,NOT LIKE '12%3' 为 false,为 true
    对于“1234”

如果的值
string_expressionpattern_value
为 NULL 或未知,则该值
LIKE 表达式未知。如果
escape_character 已指定并且是
NULL,LIKE的值
表达未知。

Use a LIKE expression. Here is a quote from the section 4.6.9 Like Expression of the JPA 1.0 spec (JSR 220):

The syntax for the use of the
comparison operator [NOT] LIKE in a
conditional expression is as follows:

string_expression [NOT] LIKE pattern_value [ESCAPE escape_character]

The string_expression must have a
string value. The pattern_value is a
string literal or a string-valued
input parameter in which an underscore
(_) stands for any single character, a
percent (%) character stands for any
sequence of characters (including the
empty sequence), and all other
characters stand for themselves. The
optional escape_character is a
single-character string literal or a
character-valued input parameter
(i.e., char or Character) and is
used to escape the special meaning of
the underscore and percent characters
in pattern_value.

Examples are:

  • address.phone LIKE ‘12%3’ is true for ‘123’ ‘12993’ and false for ‘1234’
  • asentence.word LIKE ‘l_se’ is true for ‘lose’ and false for ‘loose’
  • aword.underscored LIKE ‘\_%’ ESCAPE ‘\’ is true for ‘_foo’ and
    false for ‘bar’
  • address.phone NOT LIKE ‘12%3’ is false for ‘123’ and ‘12993’ and true
    for ‘1234’

If the value of the
string_expression or pattern_value
is NULL or unknown, the value of the
LIKE expression is unknown. If the
escape_character is specified and is
NULL, the value of the LIKE
expression is unknown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文