4
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の日付はnullにすることができます(終わりは将来無限になります)この条件を追加するにはどうすればよいですか?

ありがとう、

4

2 に答える 2

11

日付の比較を2つのブロックに分割し、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);
于 2013-03-25T08:47:36.003 に答える
-2

これは自明のはずです:

                @Override
                public Predicate toPredicate( Root<OrganizationCommission> root, CriteriaQuery<?> query,
                                              CriteriaBuilder cb )
                {
                    // The predicate validFrom - the column should contains null or values less than validityDate
                    Predicate validFrom = cb.or( root.get( OrganizationCommission_.validFrom ).isNull(),
                                                 cb.lessThanOrEqualTo(
                                                         root.get( OrganizationCommission_.validFrom ),
                                                         validityDate ) );
                    // The predicate validTo - the column should contains null or values greater than validityDate
                    Predicate validTo = cb.or( root.get( OrganizationCommission_.validTo ).isNull(),
                                               cb.greaterThanOrEqualTo( root.get( OrganizationCommission_.validTo ),
                                                                        validityDate ) );
                    Predicate predicate = cb.conjunction();
                    predicate.getExpressions().add( cb.and( validFrom, validTo ) );
                    return predicate;
                }
于 2013-06-09T12:10:20.720 に答える