EJB QL 中不区分大小写的搜索
这看起来非常简单,我不敢相信我自己还没有找到解决方案。我有一个名为 PersonBean 的 bean,它有一个名称。现在我想编写一个查找器方法,它接受一个字符串并查找名字中包含该字符串的人,不区分大小写。这是我当前的 EJB QL:
SELECT OBJECT(p)
FROM Person p
WHERE (p.name LIKE ?1)
我有两个问题:
- 我希望搜索不区分大小写(上面是区分大小写的)并且我找不到类似
lower()
或EJB QL 中的upper()
。我该怎么做才能有这种行为? 更新:我正在使用 J2EE 版本 1.4 和 glassfish 版本 2.1,如果这很重要的话。 - 我必须向该方法传递一个丑陋的字符串,即
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:
- I want the search to be case-insensitive (the above is case-sensitive) and I can't find something like
lower()
orupper()
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. - 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 likefindByString("john")
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
关于你的第二个问题,EJB QL 2.1中有一个 CONCAT 函数,所以我认为以下应该有效:
WHERE (p.name LIKE CONCAT('%', CONCAT(?1, '%')))编辑:上面的代码不起作用,因为 ql 语法只允许在
LIKE
表达式中使用文字字符串和输入参数,这实际上是限制。使用LOCATE
函数应该可以达到相同的效果,如下所示: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 theLOCATE
function like this: