EJB QL 中不区分大小写的搜索

发布于 2024-09-12 10:17:17 字数 546 浏览 5 评论 0原文

这看起来非常简单,我不敢相信我自己还没有找到解决方案。我有一个名为 PersonBean 的 bean,它有一个名称。现在我想编写一个查找器方法,它接受一个字符串并查找名字中包含该字符串的人,不区分大小写。这是我当前的 EJB QL:

SELECT OBJECT(p)
FROM Person p
WHERE (p.name LIKE ?1)

我有两个问题:

  1. 我希望搜索不区分大小写(上面是区分大小写的)并且我找不到类似 lower() 或EJB QL 中的 upper()。我该怎么做才能有这种行为? 更新:我正在使用 J2EE 版本 1.4 和 glassfish 版本 2.1,如果这很重要的话。
  2. 我必须向该方法传递一个丑陋的字符串,即 findByString("%john%").有没有办法编写 EJB QL,以便我可以传递诸如 findByString("john") 之类的内容?

This looks really simple and I can't believe I haven't found a solution myself. I have a bean named PersonBean, which has a name. Now I want to write a finder method that takes a string and looks for people with that string in their name, case-insensitively. Here is my current EJB QL:

SELECT OBJECT(p)
FROM Person p
WHERE (p.name LIKE ?1)

I have 2 problems with this:

  1. I want the search to be case-insensitive (the above is case-sensitive) and I can't find something like lower() or upper() in EJB QL. What can I do to have that behavior? UPDATE: I'm using J2EE version 1.4 and glassfish version 2.1, if that matters.
  2. I have to pass an ugly string to the method, i.e findByString("%john%"). Is there a way to write the EJB QL so that I can pass something like findByString("john")?

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

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

发布评论

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

评论(2

几度春秋 2024-09-19 10:17:17

EJB 2.x 实体 bean 中使用的 EJB-QL(不要与 JPA 和 EJB3 实体 bean 中使用的 JPQL 混淆)极其有限。尽管有 对 EJB 2.1 中的语言进行了一些添加,我认为没有上/下函数。

我不知道你运行在什么容器中,但 JBoss 对 EJB-QL(称为 JBossQL)进行了一些扩展,并具有 UCASE 功能。

EJB-QL as used in EJB 2.x entity beans (not to be confused with JPQL as used in JPA and EJB3 entity beans) is extremely limited. Altough there were some additions to the language in EJB 2.1 there are to my beliefs no upper/lower functions.

I don't know what container your running in but JBoss has made some extensions to EJB-QL (called JBossQL) and features a UCASE function.

尬尬 2024-09-19 10:17:17

关于你的第二个问题,EJB QL 2.1中有一个 CONCAT 函数,所以我认为以下应该有效:

WHERE (p.name LIKE CONCAT('%', CONCAT(?1, '%')))

编辑:上面的代码不起作用,因为 ql 语法只允许在 LIKE 表达式中使用文字字符串和输入参数,这实际上是限制。使用 LOCATE 函数应该可以达到相同的效果,如下所示:

WHERE LOCATE(p.name, ?1) <> 0

Regarding your second question, there is a CONCAT function in EJB QL 2.1, so I think the following should work:

WHERE (p.name LIKE CONCAT('%', CONCAT(?1, '%')))

Edit: The above does not work because the ql grammer only allows literal strings and input parameters in a LIKE expression, which is really limiting. It should be possible to achive the same effect by using the LOCATE function like this:

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