简体   繁体   English

如何在hql中使用相同关键字的两个字段上执行搜索操作?

[英]How to perform search operation on two fields with same keyword in hql?

I am not getting the record if any one of the field in data base is null whether the company name or location. 如果数据库中的任何字段为空(无论公司名称还是位置),我都不会得到记录。 Here keyword is what I am entering from the form. keyword是我从表单输入的内容。

This is my query: 这是我的查询:

Query qry=session.createQuery("select distinct company_name from Company_Info where company_name like '%"+keyword+"%' or  locations.city like '%"+keyword+"%'");

Does anybody know how to make this work? 有人知道如何进行这项工作吗?

Since you have to use is null in the HQL, (if there are parameters with null potential.) Use this: 由于您必须在HQL中使用null,因此(如果存在具有潜在电位的参数。)请使用以下命令:

String keywordTerm = keyword==null ? "is null" : "= :keyword";

Query qry=session.createQuery("select distinct company_name from Company_Info where company_name like '%"+keyword+"%' or  locations.city like '%"+keyword+"%'");

if(keyword!=null){
    qry.setParameter("keyword", keyword, Hibernate.STRING)
}

I achieved by using apache lucene search it's worked for me: 我通过使用Apache Lucene搜索获得了成功,对我来说很有效:

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer(Company_Info.class).startAndWait();

QueryBuilder qb = fullTextSession.getSearchFactory()
    .buildQueryBuilder().forEntity(Company.class).get();
org.apache.lucene.search.Query query = qb.keyword()
        .onFields("company_name","locations.city")
        .matching(keyword)
    .createQuery();

org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Company_Info.class);

List<Company_Info> result = hibQuery.list();

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

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