简体   繁体   English

在Spring和HQL中实现搜索功能

[英]Implementing search functionality in spring and HQL

I am implementing search in my spring boot app. 我正在我的Spring Boot应用程序中实现搜索。 Search term is searching through first name, last name and email. 搜索词是通过名字,姓氏和电子邮件进行搜索。 In my database I store name in two columns: first name and last name. 在我的数据库中,我将名称存储在两列中:名字和姓氏。 What I want to achieve is to let the user type in the full name (first name + last name) and search the database. 我要实现的是让用户键入全名(名字+姓氏)并搜索数据库。 I tried this, but when all parameters are empty this query returns me empty set, and I expected it to return all users. 我试过了,但是当所有参数为空时,此查询将返回空集,并且希望它返回所有用户。

   @Query("FROM Employee e WHERE "
            + "lower(concat(e.firstName,' ',e.lastName)) like lower('%'+':fullName'+'%') AND "
            + "lower(e.email) like lower('%'+':email'+'%')")
    public Page<Employee> search(@Param(value = "fullName") String fullName,
                                 @Param(value = "email") String email,
                                                         Pageable page);

I am not sure if I can use concat like this. 我不确定是否可以使用concat这样。 If that is a problem is there an alternative? 如果这是一个问题,是否有其他选择?

Edit 编辑

I've tried removing % signs from query and it worked. 我试着从查询中删除%符号,并且成功了。 It seems I just have to place them right, but I don't know how. 看来我只需要正确放置它们,但我不知道如何。

This worked: 这工作:

@Query("FROM Employee e WHERE "
            + "lower(concat(e.firstName,' ',e.lastName)) like lower(:fullName) AND "
            + "lower(e.email) like lower(:email)")
    public Page<Employee> search(@Param(value = "fullName") String fullName,
                                 @Param(value = "email") String email,
                                                         Pageable page);

The question is only how to use % . 问题只是如何使用%

The problem is probably that HQL interprets this part of the query in a way that does not suit your need : 问题可能是HQL以不适合您的方式解释了查询的这一部分:

lower(e.email) like lower('%'+':email'+'%')")

From the HQL specification , I know that the "+" sign is allowed as an arithmetic operator, but I'm not sure it is allowed in any other use. HQL规范中 ,我知道“ +”号可以用作算术运算符,但是我不确定在任何其他用途中都允许使用“ +”号。

Obviously, your statement "means something", as Hibernate does not report it as a syntax error, but I'm at a loss to know what it does mean, seeing we are not in an arithmetic context. 显然,您的语句“意味着某事”,因为Hibernate不会将其报告为语法错误,但由于我们不在算术上下文中,因此我不知所措。 Maybe it concatenates the string but does not replace the placeholder ? 也许它连接字符串但不替换占位符? It would be interesting to have Hibernate activate the showSQL and log it, or to see the compiled version of the query object to introspect it, but I have no time to do that right now. 让Hibernate激活showSQL并记录它,或者查看查询对象的编译版本进行自省,这将很有趣,但是我现在没有时间这样做。

Anyway, in order to achieve the same result, in a predictable way, I suggest you rewrite to 无论如何,为了达到相同的结果,以可预测的方式,我建议您重写为

mail like concat('%', lower(:email), '%')

which I find non ambiguous. 我发现这并不含糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM