Example usage for javax.persistence.criteria CriteriaBuilder notEqual

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

Introduction

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

Prototype

Predicate notEqual(Expression<?> x, Object y);

Source Link

Document

Create a predicate for testing the arguments for inequality.

Usage

From source file:org.ngrinder.agent.repository.AgentManagerSpecification.java

/**
 * Query specification to query the visible agents. "visible" means.. it's
 * visible by the agent monitor.//from www .  j av a2s .  c om
 *
 * @return Specification of this query
 */
public static Specification<AgentInfo> visible() {
    return new Specification<AgentInfo>() {
        @Override
        public Predicate toPredicate(Root<AgentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Expression<AgentControllerState> status = root.get("state").as(AgentControllerState.class);
            return cb.notEqual(status, AgentControllerState.INACTIVE);
        }
    };
}

From source file:org.ngrinder.agent.repository.AgentManagerSpecification.java

/**
 * Query specification to query the active agents.
 *
 * @return Specification of this query/* w  w  w  .  j a  v  a 2s  .  c  o  m*/
 */
public static Specification<AgentInfo> active() {
    return new Specification<AgentInfo>() {
        @Override
        public Predicate toPredicate(Root<AgentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Expression<AgentControllerState> status = root.get("state").as(AgentControllerState.class);
            return cb.and(cb.notEqual(status, AgentControllerState.INACTIVE),
                    cb.notEqual(status, AgentControllerState.UNKNOWN),
                    cb.notEqual(status, AgentControllerState.WRONG_REGION));
        }
    };
}

From source file:net.sf.companymanager.qbe.JpaUtil.java

public static <E> Predicate stringPredicate(Expression<String> path, Object attrValue, SearchMode searchMode,
        SearchParameters sp, CriteriaBuilder builder) {
    if (!sp.isCaseSensitive()) {
        path = builder.lower(path);/*  w ww  . ja v a2s .c  om*/
        //   attrValue = ((String) attrValue).toLowerCase(LocaleContextHolder.getLocale());
        attrValue = ((String) attrValue).toLowerCase();
    }
    switch (searchMode != null ? searchMode : sp.getSearchMode()) {
    case EQUALS:
        return builder.equal(path, attrValue);
    case NOT_EQUALS:
        return builder.notEqual(path, attrValue);
    case ENDING_LIKE:
        return builder.like(path, "%" + attrValue);
    case STARTING_LIKE:
        return builder.like(path, attrValue + "%");
    case ANYWHERE:
        return builder.like(path, "%" + attrValue + "%");
    case LIKE:
        return builder.like(path, (String) attrValue); // assume user
                                                       // provide the wild
                                                       // cards
    default:
        throw new IllegalStateException("expecting a search mode!");
    }
}

From source file:cc.sion.core.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//from  w w w  .  j a v a  2  s  .com
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }
                    //                  if(log.isDebugEnabled()){
                    //                     log.debug("   {}-{}-{}",names[0],filter.operator,filter.value);
                    //                  }
                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case NE:
                        predicates.add(builder.notEqual(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

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

public List<User> getAllExceptOne(User user) {
    if (user == null) {
        return this.getAll();
    }//from   w w w .ja  va 2s. c om
    List<User> users;
    logger.infof("loading all Users except %s ...", user);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> query = cb.createQuery(User.class);
    Root<User> root = query.from(User.class);
    query.where(cb.notEqual(root.get(User_.id), user.getId()));
    return em.createQuery(query).getResultList();
}

From source file:org.osiam.resource_server.storage.dao.ResourceDao.java

public <T extends ResourceEntity, V> boolean isUniqueAttributeAlreadyTaken(String attributeValue, String id,
        SingularAttribute<? super T, V> attribute, Class<T> clazz) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<T> resource = cq.from(clazz);

    cq.select(cb.countDistinct(resource));

    Predicate predicate = cb.equal(resource.get(attribute), attributeValue);
    if (id != null) {
        Predicate ignoreId = cb.notEqual(resource.get(ResourceEntity_.id), id);
        predicate = cb.and(predicate, ignoreId);
    }//w  w  w.  j a  v a2  s .c o  m
    cq.where(predicate);

    TypedQuery<Long> countQuery = em.createQuery(cq);

    return countQuery.getSingleResult() > 0;
}

From source file:com.saake.invoicer.sessionbean.PurchaseOrderFacade.java

@Override
public List<PurchaseOrder> findAll() {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();

    Root<PurchaseOrder> root = cq.from(PurchaseOrder.class);
    cq.select(root);// w ww  .ja  v  a  2s .co  m

    ParameterExpression<String> status = cb.parameter(String.class);

    //        cq.where(cb.notEqual(invRoot.get("status"), status));
    cq.where(cb.notEqual(root.get("deleted"), 'Y'));
    cq.orderBy(cb.desc(root.get("purchaseOrderId")));
    Query query = getEntityManager().createQuery(cq);
    //        query.setParameter(status, PaymentStatusEnum.DELETE.getValue());

    List<PurchaseOrder> list = query.getResultList();

    for (PurchaseOrder wo : list) {
        List<PurchaseOrderItems> tempDel = new ArrayList<>();
        for (PurchaseOrderItems wot : wo.getPurchaseOrderItems()) {
            if (wot.isDeleted()) {
                tempDel.add(wot);
            }
        }
        wo.getPurchaseOrderItems().removeAll(tempDel);
    }
    return list;
}

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

public Long waitingShippingCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);/*  w w w .  j a  v a  2 s .  c  o  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled),
            criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.paid),
            criteriaBuilder.equal(root.get("shippingStatus"), ShippingStatus.unshipped));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

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

public Long waitingPaymentCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);//from   w w w  .j a  va2s  .co m
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled));
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.or(criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.unpaid),
                    criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.partialPayment)));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}

From source file:com.expressui.sample.view.role.RoleQuery.java

@Override
public List<Predicate> buildCriteria(CriteriaBuilder builder, Root<Role> rootEntity) {
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (!isEmpty(name)) {
        ParameterExpression<String> p = builder.parameter(String.class, "name");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("name")), p));
    }//from   w w w .  java2s  .  c o  m

    if (!isEmpty(doesNotBelongToUser)) {
        ParameterExpression<User> p = builder.parameter(User.class, "doesNotBelongToUser");
        Join join = rootEntity.join("userRoles", JoinType.LEFT);
        criteria.add(builder.or(builder.notEqual(join.get("user"), p), builder.isNull(join.get("user"))));
    }

    return criteria;
}