Spring JPA,SpEL解析有效表达式,表达式中还有更多数据,'comma(,)'

发布于 2025-01-11 21:10:51 字数 760 浏览 0 评论 0原文

我必须使用 spring JPA 从 oracle 12c 检索一些特定数据,这些数据以逗号 (,) 分隔。

Ex - Branch

  ID                    Location
---------              ----------
  101                    NL,MX,USA
  102                    UK,IND,BLG
  103                    UK,NL,AUS



@Query(value = "select * from branch m where lower(m.location) like %:loc% offset :offset rows fetch next :limit rows only",nativeQuery = true)
        List<Location> getBranchListByLocation(@Param("loc") String loc,@Param("offset") int offset,@Param("limit") limit)

但是当我将 loc 值作为“NL”传递时,会发生以下错误。

   spelparseexception: el1041e: after parsing a valid expression, there is still more data in the expression: 'comma(,)'

有其他方法可以解决该问题吗?

I have to retrieve some specific data from oracle 12c using spring JPA which are separated by comma (,).

Ex - Branch

  ID                    Location
---------              ----------
  101                    NL,MX,USA
  102                    UK,IND,BLG
  103                    UK,NL,AUS



@Query(value = "select * from branch m where lower(m.location) like %:loc% offset :offset rows fetch next :limit rows only",nativeQuery = true)
        List<Location> getBranchListByLocation(@Param("loc") String loc,@Param("offset") int offset,@Param("limit") limit)

But when i pass loc value as "NL" then the following error has occurred.

   spelparseexception: el1041e: after parsing a valid expression, there is still more data in the expression: 'comma(,)'

Is there an alternative way to solve the issue?

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

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

发布评论

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

评论(1

昔梦 2025-01-18 21:10:51

% 字符需要包含在字符串文字中,并与 :loc 绑定变量连接:

select *
from   branch m
where  lower(m.location) like '%' || :loc || '%'
offset :offset rows
fetch next :limit rows only

或者,您可以将 % 前置并附加到Java 中的 loc 变量,然后只需使用 :loc 而无需在 SQL 查询中包装 %s。

(注意:您使用的是 lower,因此您希望传入 nl 而不是 NL。)


但是,您会发现如果您传递 :locin 那么它将匹配 in 以及 ind墨水。如果您想匹配完整的分隔项,那么您需要在匹配中包含周围的逗号:

select *
from   branch m
where  ',' || lower(m.location) || ',' like '%,' || :loc || ',%'
offset :offset rows
fetch next :limit rows only

The % characters need to be contained in string literals and concatenated with the :loc bind variable:

select *
from   branch m
where  lower(m.location) like '%' || :loc || '%'
offset :offset rows
fetch next :limit rows only

Alternatively, you can prepend and append % to the loc variable in Java and then just use :loc without the wrapping %s in the SQL query.

(Note: you are using lower so you want to pass in nl and not NL.)


However, you will find if you pass the :loc value in then it will match in as well as ind and ink. If you want to match the full delimited item then you want to include the surrounding commas in the match:

select *
from   branch m
where  ',' || lower(m.location) || ',' like '%,' || :loc || ',%'
offset :offset rows
fetch next :limit rows only
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文