Example usage for javax.persistence.criteria CriteriaBuilder not

List of usage examples for javax.persistence.criteria CriteriaBuilder not

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder not.

Prototype

Predicate not(Expression<Boolean> restriction);

Source Link

Document

Create a negation of the given restriction.

Usage

From source file:com.expressui.core.dao.security.query.RelatedUsersQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(role)) {
        ParameterExpression<Role> roleExp = builder.parameter(Role.class, "role");
        predicates.add(builder.equal(user.join("userRoles").get("role"), roleExp));
    }/*  w ww. j  av a  2  s .c o m*/

    if (hasValue(doesNotBelongToRole)) {
        Subquery<User> subquery = query.subquery(User.class);
        Root userRole = subquery.from(UserRole.class);
        ParameterExpression<Role> roleExp = builder.parameter(Role.class, "doesNotBelongToRole");
        subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), roleExp));
        predicates.add(builder.not(user.in(subquery)));
    }

    return predicates;
}

From source file:com.expressui.core.dao.security.query.RoleQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Role> query, Root<Role> role) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(name)) {
        ParameterExpression<String> nameExp = builder.parameter(String.class, "name");
        predicates.add(builder.like(builder.upper(role.<String>get("name")), nameExp));
    }/*from  w  w  w.j  ava  2 s  .com*/

    if (hasValue(doesNotBelongToUser)) {
        Subquery<Role> subquery = query.subquery(Role.class);
        Root<UserRole> userRole = subquery.from(UserRole.class);
        ParameterExpression<User> userExp = builder.parameter(User.class, "doesNotBelongToUser");
        subquery.select(userRole.<Role>get("role")).where(builder.equal(userRole.get("user"), userExp));
        predicates.add(builder.not(role.in(subquery)));
    }

    return predicates;
}

From source file:com.expressui.core.dao.security.query.UserQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) {
    List<Predicate> predicates = new ArrayList<Predicate>();

    if (hasValue(loginName)) {
        ParameterExpression<String> loginNameExp = builder.parameter(String.class, "loginName");
        predicates.add(builder.like(builder.upper(user.<String>get("loginName")), loginNameExp));
    }/*from   www  .j av a2  s .c  o m*/

    if (hasValue(doesNotBelongToRole)) {
        Subquery<User> subquery = query.subquery(User.class);
        Root userRole = subquery.from(UserRole.class);
        ParameterExpression<Role> role = builder.parameter(Role.class, "doesNotBelongToRole");
        subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), role));
        predicates.add(builder.not(user.in(subquery)));
    }

    return predicates;
}

From source file:org.oncoblocks.centromere.jpa.QueryCriteriaSpecification.java

public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    String key = queryCriteria.getKey();
    Object value = queryCriteria.getValue();
    Evaluation eval = queryCriteria.getEvaluation();
    Path path = null;/*w  w w  .  j  a  v  a  2 s  .c o  m*/
    if (key.contains(".")) {
        String[] bits = key.split("\\.");
        path = root.join(bits[0]).get(bits[1]);
    } else {
        path = root.get(key);
    }
    logger.debug(String.format("[CENTROMERE] Converting QueryCriteria to JPA specification: %s",
            queryCriteria.toString()));
    switch (eval) {
    case EQUALS:
        return criteriaBuilder.equal(path, value);
    case NOT_EQUALS:
        return criteriaBuilder.notEqual(path, value);
    case IN:
        return path.in(value);
    case NOT_IN:
        return criteriaBuilder.not(path.in(value));
    case IS_NULL:
        return criteriaBuilder.isNull(path);
    case NOT_NULL:
        return criteriaBuilder.isNotNull(path);
    case GREATER_THAN:
        return criteriaBuilder.greaterThan(path, value.toString());
    case GREATER_THAN_EQUALS:
        return criteriaBuilder.greaterThanOrEqualTo(path, value.toString());
    case LESS_THAN:
        return criteriaBuilder.lessThan(path, value.toString());
    case LESS_THAN_EQUALS:
        return criteriaBuilder.lessThanOrEqualTo(path, value.toString());
    case BETWEEN:
        return criteriaBuilder.and(criteriaBuilder.greaterThan(path, ((List<?>) value).get(0).toString()),
                criteriaBuilder.lessThan(path, ((List<?>) value).get(1).toString()));
    case OUTSIDE:
        return criteriaBuilder.or(criteriaBuilder.greaterThan(path, ((List<?>) value).get(1).toString()),
                criteriaBuilder.lessThan(path, ((List<?>) value).get(0).toString()));
    case BETWEEN_INCLUSIVE:
        return criteriaBuilder.and(
                criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(0).toString()),
                criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(1).toString()));
    case OUTSIDE_INCLUSIVE:
        return criteriaBuilder.or(
                criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(1).toString()),
                criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(0).toString()));
    case LIKE:
        return criteriaBuilder.like(path, "%" + value.toString() + "%");
    case NOT_LIKE:
        return criteriaBuilder.notLike(path, "%" + value.toString() + "%");
    case STARTS_WITH:
        return criteriaBuilder.like(path, value.toString() + "%");
    case ENDS_WITH:
        return criteriaBuilder.like(path, "%" + value.toString());
    default:
        return criteriaBuilder.equal(root.get(queryCriteria.getKey()), queryCriteria.getValue());
    }
}

From source file:eu.uqasar.service.user.UserService.java

public List<User> getAllExceptAndFilter(Collection<User> usersToExclude, final String filter) {
    if (usersToExclude == null || usersToExclude.isEmpty()) {
        return this.getAll();
    }//from w w w  . ja  va 2  s . c  o m
    logger.infof("loading all Users except %s ...", usersToExclude);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    if (filter != null && !filter.isEmpty()) {

        Expression<String> literal = cb.upper(cb.literal(LIKE_WILDCARD + filter + LIKE_WILDCARD));
        Predicate likeFirstName = cb.like(cb.upper(root.get(User_.firstName)), literal);
        Predicate likeLastName = cb.like(cb.upper(root.get(User_.lastName)), literal);
        Predicate likeUserName = cb.like(cb.upper(root.get(User_.userName)), literal);
        Predicate orLikeName = cb.or(likeFirstName, likeLastName, likeUserName);
        query.where(cb.and(cb.not(root.in(usersToExclude)), orLikeName));
    } else {
        query.where(cb.not(root.in(usersToExclude)));
    }
    return em.createQuery(query).getResultList();
}

From source file:com.dbs.sdwt.jpa.ByPropertySelectorUtil.java

private <E> void byObjectOrModeSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates,
        SearchParameters sp, PropertySelector<? super E, ?> selector) {
    List<Predicate> selectorPredicates = newArrayList();
    Path<?> path = jpaUtil.getPath(root, selector.getAttributes());
    List<?> selected = selector.getSelected();
    if (selected.contains(null)) {
        selected = newArrayList(selector.getSelected());
        selected.remove(null);/* w  w w  .  ja  va 2s .  c o  m*/
        selectorPredicates.add(builder.isNull(path));
    }
    if (isNotEmpty(selected)) {
        if (selected.get(0) instanceof Identifiable) {
            List<Serializable> ids = newArrayList();
            for (Object selection : selected) {
                ids.add(((Identifiable<?>) selection).getId());
            }
            if (SearchMode.NOT_IN == selector.getSearchMode()) {
                selectorPredicates.add(builder.not(path.get("id").in(ids)));
            } else {
                selectorPredicates.add(path.get("id").in(ids));
            }
        } else {
            if (SearchMode.NOT_IN == selector.getSearchMode()) {
                selectorPredicates.add(builder.not(path.in(selected)));
            } else {
                selectorPredicates.add(path.in(selected));
            }

        }
    }

    predicates.add(jpaUtil.orPredicate(builder, selectorPredicates));
}

From source file:hr.diskobolos.persistence.impl.EvaluationAnswerPersistenceImpl.java

@Override
public Long fetchNumberOfMemberRegistersWithoutTerms() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<MemberRegister> memberRegister = cq.from(MemberRegister.class);
    Subquery<Long> sq = cq.subquery(Long.class);
    Root<EvaluationAnswer> evaluationAnswer = sq.from(EvaluationAnswer.class);
    Join<EvaluationAnswer, QuestionChoicesDef> choiceDef = evaluationAnswer.join(EvaluationAnswer_.answer);
    Join<QuestionChoicesDef, EvaluationQuestionDef> questionDef = choiceDef
            .join(QuestionChoicesDef_.evaluationQuestionDef);
    ParameterExpression<QuestionnaireType> questionnaireType = cb.parameter(QuestionnaireType.class,
            "questionnaireType");
    sq.select(evaluationAnswer.get("memberRegister").get("id"))
            .where(cb.equal(questionDef.get(EvaluationQuestionDef_.questionnaireType), questionnaireType));
    cq.select(cb.count(memberRegister.get("id"))).where(cb.not(cb.in(memberRegister.get("id")).value(sq)));
    TypedQuery<Long> query = entityManager.createQuery(cq);
    query.setParameter("questionnaireType", QuestionnaireType.TERMS_OF_CONDITION);
    return query.getSingleResult();
}

From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Predicate generatePredicate(Root root, CriteriaBuilder builder,
        QueryParameter... queryParameters) {
    Predicate condition = builder.conjunction();
    for (QueryParameter queryParameter : queryParameters) {
        Object value = queryParameter.getValue();
        if (value == null || StringUtils.isBlank(value.toString())) {
            continue;
        }/*  w  ww .j  a  v a 2  s .  c  o m*/
        Path path = root.get(queryParameter.getKey());
        switch (queryParameter.getMode()) {
        case BETWEEN:
            Object[] values = asArray(value);
            if (values != null) {
                condition = builder.and(builder.between((Path<Comparable>) path, asComparable(values[0]),
                        asComparable(values[1])));
            }
            break;
        case GREATER_THAN:
            condition = builder.and(condition,
                    builder.greaterThan((Path<Comparable>) path, asComparable(value)));
            break;
        case GREATER_EQUALS:
            condition = builder.and(condition,
                    builder.greaterThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_THAN:
            condition = builder.and(condition, builder.lessThan((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_EQUALS:
            condition = builder.and(condition,
                    builder.lessThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case IS_NULL:
            condition = builder.and(condition, builder.isNull(path));
            break;
        case IS_NOT_NULL:
            condition = builder.and(condition, builder.isNotNull(path));
            break;
        case IN:
            condition = builder.and(condition, path.in(asArray(value)));
            break;
        case NOT_IN:
            condition = builder.and(condition, builder.not(path.in(asArray(value))));
            break;
        case LIKE:
            condition = builder.and(condition, builder.like(path, "%" + String.valueOf(value) + "%"));
            break;
        case NOT_LIKE:
            condition = builder.and(condition, builder.notLike(path, "%" + String.valueOf(value) + "%"));
            break;
        case EQUALS:
            condition = builder.and(condition, builder.equal(path, value));
            break;
        case NOT_EQUALS:
            condition = builder.and(condition, builder.notEqual(path, value));
            break;
        default:
            break;
        }
    }
    return condition;
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public List<Product> findList(Goods goods, Set<Product> excludes) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);/*  w w  w.  ja  va2  s  .c om*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (goods != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("goods"), goods));
    }
    if (excludes != null && !excludes.isEmpty()) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes)));
    }
    criteriaQuery.where(restrictions);
    return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList();
}

From source file:com.yunguchang.data.ApplicationRepository.java

public List<TAzCarinfoEntity> listAllCandidateCars(String[] applicationIds, String keyword, Integer offset,
        Integer limit, PrincipalExt principalExt) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TAzCarinfoEntity> cq = cb.createQuery(TAzCarinfoEntity.class);
    Root<TAzCarinfoEntity> carRoot = cq.from(TAzCarinfoEntity.class);
    carRoot.fetch(TAzCarinfoEntity_.driver, JoinType.LEFT);

    cq.select(carRoot);//from w  w  w  . j  a v a2s.c o  m
    Subquery<TBusScheduleCarEntity> overlapScheduleCarSubQuery = cq.subquery(TBusScheduleCarEntity.class);

    applyOverlapScheduleCarSubquery(overlapScheduleCarSubQuery, applicationIds, carRoot, null, cb,
            principalExt);
    Predicate predicate = cb.and(cb.equal(carRoot.get(TAzCarinfoEntity_.clzt), "02"),
            cb.or(cb.equal(carRoot.get(TAzCarinfoEntity_.repairingState), RepairingState.NONE.id()),
                    cb.isNull(carRoot.get(TAzCarinfoEntity_.repairingState))

            ), cb.not(cb.exists(overlapScheduleCarSubQuery))

    );

    if (keyword != null) {
        predicate = cb.and(predicate,
                cb.or(cb.like(carRoot.get(TAzCarinfoEntity_.sysOrg).get(TSysOrgEntity_.orgname),
                        "%" + keyword + "%"),
                        cb.like(carRoot.get(TAzCarinfoEntity_.cphm), "%" + keyword + "%")));
    }

    cq.where(predicate);
    cq.orderBy(cb.asc(carRoot.get(TAzCarinfoEntity_.cphm)));
    TypedQuery<TAzCarinfoEntity> query = em.createQuery(cq);
    if (offset != null) {
        query.setFirstResult(offset);
    }

    if (limit != null) {
        query.setMaxResults(limit);
    }

    applySecurityFilter("cars", principalExt);

    return query.getResultList();
}