0

我有一个查询数据的 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<java.lang.Boolean> x, Expression<java.lang.Boolean> y)文档中的这一部分。

4

1 回答 1

0

对我来说,这看起来只是你的括号有问题。您的谓词不是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 + "%")
    )
);  
于 2015-10-01T11:42:28.557 回答