【问题标题】:criteria query is not getting data when adding or condition添加或条件时条件查询未获取数据
【发布时间】:2015-10-01 08:27:45
【问题描述】:

我有一个查询数据的 Java 文件,我正在尝试添加过滤功能。通过使用标准构建器和标准查询,我设法获取了我想要过滤的数据。

首先,这是显示我的数据的列:

Name              Host
Fikrie            ubuntu
Fikrie2           unix
Fikrie3           ulalala
Fikrie4           ugagaga

这里使用了 3 个变量。名称列显示来自名称的数据。对于 Host 列,这有点棘手。它将显示主机名,但如果有 logAsHost 显示,此数据将覆盖主机名。

这就是我的数据的真实样子:

Name              Host
Fikrie            ubuntu      <-- hostname = 1, logAsHost = ubuntu
Fikrie2           unix        <-- hostname = 123, logAsHost = unix
Fikrie3           ulala       <-- hostname = ulala, logAsHost = no value
Fikrie4           ugaga       <-- hostname = ugaga, logAsHost = no value

当我尝试只过滤 1 个变量时,我设法做到了。 (例如,按名称过滤)。当我尝试过滤掉 2 个变量时,就会出现问题。我没有设法获得任何数据。

这是我使用的代码:

public List<Connection> retrieveAll(String nameFilter, String hostFilter,
        int start, int length) {
    ServiceUtil.requireAdmin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Connection> q = cb.createQuery(Connection.class);
    Root<Connection> c = q.from(Connection.class);
    q.select(c);

    logger.info("nameFilter = [" + nameFilter + "]");
    logger.info("hostFilter = [" + hostFilter + "]");

    //This is the line that I use to query data.
    //when I replace Connection_.hostname with Connection_.logAsHost, or Connection_.name
    //It works just fine. So I use either one of these line to query.
    //q.where(cb.like(c.get(Connection_.hostname), "%" + hostFilter + "%"));
    //q.where(cb.like(c.get(Connection_.logAsHost), "%" + hostFilter + "%"));
    //q.where(cb.like(c.get(Connection_.name), "%" + nameFilter + "%"));

    //This is the problem part.
    //When I add cb.or, it cannot get any data.
    //From the documentation, it should just be q.where(cb.or(A, B))
    //Where A is the first expression and B is the second.
    // I have confirm both the expression are working by calling it separately
     q.where(cb.or(cb.like(c.get(Connection_.hostname), "%" + hostFilter + "%")), cb.like(c.get(Connection_.logAsHost), "%" + hostFilter + "%"));

    List<Connection> results = em.createQuery(q).setFirstResult(start)
            .setMaxResults(length).getResultList();

    for (Connection conn : results) {
        logger.info("Name=" + conn.getName() + ", hostname=["
                + conn.getHostname() + "]" + ", logAsHost =["
                + conn.getLogAsHost() + "]");
    }

    return results;
}

此日志显示数据是否可用:

我使用了c.get(Connection_.hostname),并将u传递给了hostFilter,

INFO nameFilter = []
INFO hostFilter = [u]
INFO Name=fikrie3, hostname=[ulala], logAsHost =[]
INFO Name=testt, hostname=[ugaga], logAsHost =[]

我使用c.get(Connection_.logAsHost),并将u传递给hostFilter,

INFO nameFilter = []
INFO hostFilter = [u]
INFO Name=fikrie, hostname=[192.168.56.90], logAsHost =[ubuntu]
INFO Name=fikrie2, hostname=[192.168.56.90], logAsHost =[unix]

我将两者结合并将u 传递给hostFilter,

 INFO nameFilter = []
 INFO hostFilter = [u]

我假设cb.or() 导致了这个错误。如果是这样,如何在条件查询中正确使用 OR 条件?我正在关注这部分 or(Expression&lt;java.lang.Boolean&gt; x, Expression&lt;java.lang.Boolean&gt; y) 来自文档。

【问题讨论】:

    标签: java criteria-api criteriaquery


    【解决方案1】:

    对我来说,这看起来只是你的括号有问题。您的谓词不是or 方法的参数

    你有

    q.where(
        cb.or(
            cb.like(c.get(Connection_.hostname), "%" + hostFilter + "%")
        ),
        cb.like(c.get(Connection_.logAsHost), "%" + hostFilter + "%")
    );
    

    这导致两个谓词的AND

    应该是

    q.where(
        cb.or(
            cb.like(c.get(Connection_.hostname), "%" + hostFilter + "%"),
            cb.like(c.get(Connection_.logAsHost), "%" + hostFilter + "%")
        )
    );  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      相关资源
      最近更新 更多