声明

时间:2016-01-27 14:02:11

标签: java mysql jpa criteria

我想在CriteriaQuery中实现以下where语句。

  (Event.time >= NOW() 
     OR (Event.time >= '$clubHours' && (`EventSelectType`.`type`='cafe' || `EventSelectType`.`type`='DJ')) 
     OR (Event.endtime >= NOW() && (`EventSelectType`.`type`='festival' OR `EventSelectType`.`type`='dj' OR `EventSelectType`.`type`='cafe')))

我尝试了以下内容;

eQr.where(
        cB.or(
            cB.or(
                    cB.greaterThanOrEqualTo(eventRoot.<Date>get("time"), curDate)   
                ),
            cB.or(
                    cB.and(
                    cB.greaterThanOrEqualTo(eventRoot.<Date>get("time"), cal.getTime())
                    ),
                        cB.or(
                                cB.equal(eventType.<String>get("type"), "cafe"),
                                cB.equal(eventType.<String>get("type"), "DJ")
                                )
                ),
            cB.or(
                    cB.and(
                    cB.greaterThanOrEqualTo(eventRoot.<Date>get("endtime"), curDate)
                    ),
                        cB.or(
                                cB.equal(eventType.<String>get("type"), "festival"),
                                cB.equal(eventType.<String>get("type"), "DJ"),
                                cB.equal(eventType.<String>get("type"), "cafe")
                                )
                )
        ),
        cB.equal(eventRoot.<Integer>get("id"), eventID),
        cB.equal(eventRoot.<Integer>get("active"), 1)
        );

但所有这些都是一个很大的OR语句

(event0_.time>=? or event0_.time>=? or eventselec13_.type=? or eventselec13_.type=? or event0_.endtime>=? or eventselec13_.type=? or eventselec13_.type=? or eventselec13_.type=?)

我怎样才能得到我正在寻找的结果?

修改

我目前有这个

Predicate eventTypeIscafeORDJ = cB.or(
        cB.equal(eventType.<String>get("type"), "cafe"),
        cB.equal(eventType.<String>get("type"), "DJ")
    );
Predicate eventTypeIsFestivalDJORCafe = cB.or(
        cB.equal(eventType.<String>get("type"), "cafe"),
        cB.equal(eventType.<String>get("type"), "DJ"),
        cB.equal(eventType.<String>get("type"), "festival")
        );

    Predicate timeGreaterOrEqualTo = cB.greaterThanOrEqualTo(eventRoot.<Date>get("time"), cal.getTime());
    Predicate endTimeGreaterOrEqualTo = cB.greaterThanOrEqualTo(eventRoot.<Date>get("endtime"), curDate);
    Predicate equalToId = cB.equal(eventRoot.<Integer>get("id"), eventID);

    eQr.where(timeGreaterOrEqualTo, cB.or( cB.and( timeGreaterOrEqualTo, eventTypeIscafeORDJ ), cB.and(endTimeGreaterOrEqualTo, eventTypeIsFestivalDJORCafe )), equalToId);

哪个输出

where event0_.time>=? and (event0_.time>=? and (eventselec13_.type=? or eventselec13_.type=?) or event0_.endtime>=? and (eventselec13_.type=? or eventselec13_.type=? or eventselec13_.type=?)) and event0_.id=633188

但必须是

where (event0_.time>=? OR (event0_.time>=? and (eventselec13_.type=? or eventselec13_.type=?)) or (event0_.endtime>=? and (eventselec13_.type=? or eventselec13_.type=? or eventselec13_.type=?))) and event0_.id=633188

它仍然不是我想要的东西,我似乎无法按照我想要的方式工作。

1 个答案:

答案 0 :(得分:1)

从明显的第一个问题开始:

var waitElementFound = function (value) {
    var EC = protractor.ExpectedConditions;
    browser.wait(EC.visibilityOf(value), 35000);
};

module.exports = new waitElementFound(); //export the function

这应该构造为

Event.time >= '$clubHours' && (`EventSelectType`.`type`='cafe' || `EventSelectType`.`type`='DJ')) 

相反,你有:

cB.and(
    cB.greaterThanOrEqualTo(eventRoot.<Date>get("time"), cal.getTime()), 
    cB.or(
        cB.equal(eventType.<String>get("type"), "cafe"),
        cB.equal(eventType.<String>get("type"), "DJ")
    )
), ...

因此,基本上,您需要确保查看括号而不是缩进,以便了解代码的作用。作为旁注,您最好逐步构造此谓词,以便您的代码更易于阅读和理解。 E.g:

cB.and(
    cB.greaterThanOrEqualTo(eventRoot.<Date>get("time"), cal.getTime())
),
cB.or(
    cB.equal(eventType.<String>get("type"), "cafe"),
    cB.equal(eventType.<String>get("type"), "DJ")
)

数据库几乎总是代码的较慢部分,优化器可能会生成相同的运行时或任何一组代码,因此您应该让自己和其他人更容易。此外,这是调试编码问题的经过时间考验的方式,特别是当你看到所有这些问题时。