gpt4 book ai didi

JPA 使用 Criteria 查找日期在开始日期和结束日期之间且可以为空的实体

转载 作者:行者123 更新时间:2023-12-04 23:04:35 26 4
gpt4 key购买 nike

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery cQuery = builder.createQuery();
Root<AcsTemplateDateJPA> root = cQuery.from(AcsTemplateDateJPA.class);
ParameterExpression<Date> d = builder.parameter(Date.class);
cQuery.where(builder.between(d, root.<Date>get("inservicedate"), root.<Date>get("outservicedate")));
Query query = em.createQuery(cQuery.select(root.get("acstemplateid"))).setParameter(d, planDate, TemporalType.DATE);
List results =query.getResultList();

这里 outService 日期可以为空( future 无限期结束)
我如何添加这个条件?

谢谢,

最佳答案

我会将日期比较分成两个块,并测试是否 outservicedate isNull() .就像是:

Predicate startPredicate = builder.greaterThanOrEqualTo(root.<Date>get("inservicedate"), d);
Path<Date> outServiceDate = root.<Date>get("outservicedate");
Predicate endPredicate = builder.or(builder.isNull(outServiceDate), builder.lessThanOrEqualTo(outServiceDate, d));
Predicate finalCondition = builder.and(startPredicate, endPredicate);

关于JPA 使用 Criteria 查找日期在开始日期和结束日期之间且可以为空的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com