基于条件的Hibernate查询构建

Vic*_*cky 5 java orm hibernate jpa

我有一个表单,用户可以在其中选择搜索条件。

标准是:

Product Name: Input field
Name Option: Radio button group - begins with (default selected)/ is/ contains
Country: dropdown of country
Status: All, Active, Blocked
Type: All, One, Two, Three
Run Code Online (Sandbox Code Playgroud)

只有产品名称是强制性的。其他下拉菜单是可选的。

所以如果没有给出国家,我应该找到所有国家的产品。如果没有给出 active ,我应该同时找到有效和被阻止的产品。如果没有给出类型,我应该返回所有三种类型的产品。

我正在构建休眠查询,如下所示:

String productName = searchCriteria.getValue("productName");
String productNameCriteria = searchCriteria.getValue("productNameCriteria");
String country = searchCriteria.getValue("country");
String status = searchCriteria.getValue("status");
String type = searchCriteria.getValue("type");

Query prodQuery = null;
String prodSql = "select count(*) from Product p where";

// is
if (productNameCriteria.equalsIgnoreCase("IS")){
            prodSql += "p.productName = '"+productName+"'";
        }

// begins with
else if (productNameCriteria.equalsIgnoreCase("BEGINS WITH")){
            prodSql += "p.productName = '"+productName+"%'";
        }

// contains
else (productNameCriteria.equalsIgnoreCase("BEGINS WITH")){
            prodSql += "p.productName = '%"+productName+"%'";

        }

if(!country.equalsIgnoreCase("0")){
         prodSql += " and p.country = '"+country+"'";
     }

if(!status.equalsIgnoreCase("ALL")){
      if(status.equalsIgnoreCase("active"))
          prodSql += " and p.status = 'active'";

      else
          prodSql += " and p.status = 'blocked'";
 }

 if(!type.equalsIgnoreCase("ALL")){
      if(type.equalsIgnoreCase("one"))
          prodSql += " and p.type = 'one'";

      else if(type.equalsIgnoreCase("two"))
          prodSql += " and p.type = 'two'";

      else 
         prodSql += " and p.type = 'three'";
}

prodQuery = this.em.createQuery(prodSql);
List<Object[]> results = prodQuery.getResultList();
Run Code Online (Sandbox Code Playgroud)

我是否以正确的方式进行查询构建?或者有没有其他有效的方法???

谢谢阅读!!

pto*_*mli 6

尝试查看Criteria Query

Criteria crit = sess.createCriteria(Product.class);
if (productNameCriteria.equalsIgnoreCase("IS"))
    crit.add( Restrictions.eq("productName", productName);
else if (productNameCriteria.equalsIgnoreCase("BEGINS WITH"))
    crit.add( Restrictions.like("productName", productName + "%")
// etc
Run Code Online (Sandbox Code Playgroud)

如果您绝对必须构建一个字符串查询,那么您应该使用 StringBuilder

StringBuilder sb = new StringBuilder();
sb.append("select count(*) from Product p where ");
if (productNameCriteria.equalsIgnoreCase("IS"))
    sb.append("p.productName = '").append(productName).append("'");
// etc
String query = sb.toString();
Run Code Online (Sandbox Code Playgroud)

使用 aStringBuilder减少了在运行时创建的实例数。

你也可以考虑使用查询参数,这会降低一些查询的复杂性,虽然我不知道运行时查询性能的影响是什么。

"select count(*) from Product p where p.productName = :productName"
"select count(*) from Product p where p.productName = ?"
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用Query#setParameter(或其他变体之一,如setString)来定义查询中的值。这也是构建查询的好得多的方法,因为它将自动管理您从 UI 接收到的值的引用和转义。使用查询参数而不是字符串连接,无论您如何构建查询字符串。