如何通过给定的 java 布尔值添加条件谓词?

How can I add conditional predicates by given java boolean?

我有一个应该有条件地添加谓词的业务方法。

public <T extends BaseEntity, Y> T findEntityByUniqueAttribute(
        final Class<T> entityType,
        final Supplier<SingularAttribute<? super T, Y>> attributeSupplier,
        final Supplier<? extends Y> valueSupplier,
        final boolean filterDeleted) {
    return applyWithPersistenceContext(manager -> {
        final CriteriaBuilder builder = manager.getCriteriaBuilder();
        final CriteriaQuery<T> criteria
                = builder.createQuery(entityType);
        final Root<T> from = criteria.from(entityType);
        criteria.select(from);
        criteria.where(builder.equal(from.get(attributeSupplier.get()),
                                     valueSupplier.get()));
        final TypedQuery<T> query = manager.createQuery(criteria);
        try {
            return Optional.of(query.getSingleResult())
                    .filter(result -> !filterDeleted
                                      || result.getDeleted() != null)
                    .orElse(null);
        } catch (final NoResultException nre) {
            return null;
        }
    });
}

如您所见,此方法确实进行了提取和检查。 现在我相信我可以通过将 post 过滤器(!filterDeleted || result.getDeleted() != null)添加到查询本身来改进这一点。

我该怎么做?

到目前为止,我做到了。

        final List<Predicate> predicates = new ArrayList<>(2);
        predicates.add(builder.equal(from.get(attributeSupplier.get()),
                                     valueSupplier.get()));
        if (filterDeleted) {
            predicates.add(builder.isNull(from.get(BaseEntity_.deleted)));
        }
        criteria.where(predicates.toArray(new Predicate[predicates.size()]));
        final TypedQuery<T> query = manager.createQuery(criteria);
        try {
            return query.getSingleResult();
        } catch (final NoResultException nre) {
            return null;
        }

有什么方法可以用给定的 filterDeleted 制作 Predicate 吗?

您可以像这样为布尔参数创建谓词为 false

ParameterExpression<Boolean> param = builder.parameter(Boolean.class, "myParam");
Predicate paramFalse = builder.isFalse(param);

然后以正常方式将参数设置为查询的输入。