条件查询在添加或条件时未获取数据



我有一个查询数据的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

当我试图只过滤一个变量时,我设法做到了。当我试图过滤掉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传递给主机筛选器、

 INFO nameFilter = []
 INFO hostFilter = [u]

我假设cb.or()导致了这个错误。如果是,如何在criteriaquery中正确使用OR条件?我正在关注这一部分or(Expression<java.lang.Boolean> x, Expression<java.lang.Boolean> y)

对我来说,这似乎只是括号的问题。您的谓词不是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 + "%")
    )
);  

相关内容

  • 没有找到相关文章

最新更新