EclipseLink:Criteria Builder API和NOT IN子句

时间:2016-06-06 12:44:47

标签: jpa eclipselink

我正在使用条件构建器查询,并且遇到包含“not in”子句的问题。这是我的代码:

CriteriaBuilder cb = AppUtils.EMF.PU_ROSTER.getEntityManager().getCriteriaBuilder();

    CriteriaQuery<Members> cq = cb.createQuery(Members.class);

    Root<Members> root = cq.from(Members.class);
    Expression<Integer> idExp = root.get(Members_.memberId);

    if (currentFlag) {
        Predicate p = idExp.in(getCurrentUserList());
        cq.where(cb.and(p));
    } else {
        Predicate p = idExp.in(getCurrentUserList());
        cq.where(cb.not(cb.and(p)));

    }

    cq.where(cb.and(root.get(Members_.refMemberStatus).get(RefMemberStatus_.id).in(AppUtils.MEMBER_STATUS_ACTIVE)));

    List<Order> orderList = new ArrayList();
    orderList.add(cb.asc(root.get(Members_.lastName)));
    orderList.add(cb.asc(root.get(Members_.firstName)));

    cq.orderBy(orderList);

    Query q = AppUtils.EMF.PU_ROSTER.getEntityManager().createQuery(cq);

    return q;
}

getCurrentUserList()函数返回List<Integer>个集合。我的问题是无论我的'currentFlag'是真还是假,我都得到相同的结果集。查看调试器中的查询,它们看起来完全相同。我在currentFlag上设置了一个断点并且它具有正确的值,并且实际上根据它的值处理了相应的代码,但查询是相同的。

[EL Fine]: sql: 2016-06-06 07:41:34.26--ServerSession(845434732)--Connection(733865607)--SELECT MEMBER_ID, ALT_PHONE, CDATE, CITY, DEGREE, DEPARTMENT, EDATE, EMAIL, FAX, FIRST_NAME, INSTITUTION, LAST_NAME, MIDDLE_NAME, MOBILE_PHONE, MODIFIER, PAGER, PASSWORD, PHONE, PHONE_EXT, RAW_PASSWORD, STATE, STREET1, STREET2, USERNAME, ZIP, MEMBER_STATUS_ID FROM ROSTER.MEMBERS WHERE (MEMBER_STATUS_ID IN (?)) ORDER BY LAST_NAME ASC, FIRST_NAME ASC

[EL Fine]: sql: 2016-06-06 07:42:09.191--ServerSession(845434732)--Connection(733865607)--SELECT MEMBER_ID, ALT_PHONE, CDATE, CITY, DEGREE, DEPARTMENT, EDATE, EMAIL, FAX, FIRST_NAME, INSTITUTION, LAST_NAME, MIDDLE_NAME, MOBILE_PHONE, MODIFIER, PAGER, PASSWORD, PHONE, PHONE_EXT, RAW_PASSWORD, STATE, STREET1, STREET2, USERNAME, ZIP, MEMBER_STATUS_ID FROM ROSTER.MEMBERS WHERE (MEMBER_STATUS_ID IN (?)) ORDER BY LAST_NAME ASC, FIRST_NAME ASC

我已经尝试将我的cb.not子句放在不同的地方,但似乎没有什么区别。

根据下面的评论(谢谢 Neil Stockton ),我使用以下内容更新了上述代码的一部分:

if (currentFlag) {
        Predicate p = idExp.in(getCurrentUserList());
        cq.where(p);
    } else {
        Predicate p = idExp.in(getCurrentUserList());
        cq.where(cb.not(p));

    }

结果集或生成的查询没有变化..

1 个答案:

答案 0 :(得分:0)

好的,再次谢谢你,尼尔!你让我更多地考虑它,我想出了发生了什么......这是我的解决方案:

library("png") 
library("grid") 
Rlogo <- readPNG("Rlogo.png")

par(mfrow=c(1,2))
barplot(-(1:10), horiz=T, border=NA, axes=FALSE,)
grid.clip()
grid.draw(rasterGrob(Rlogo, x=unit(0.5, "npc"), y = unit(0.5, "npc"),
          width=unit(0.1, "npc"), height=unit(0.1, "npc")))
barplot(1:10, horiz=T, border=FALSE, axes=F)

事实证明,当我设置where子句时,它不会附加到具有new的属性,但会完全覆盖该值(Duh!)...所以我为所有条件创建了相应的谓词,并观察了它工作。如果有更动态的方法,请随时告诉我。我只是想出这些东西......