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:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public List<ActivityAIPC> itemsDateRange(Date start, Date end, int[] range) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));
    cq.orderBy(cb.asc(rt.get(ActivityAIPC_.fromdateofservice)));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);//from  w  w  w  .j av  a 2 s  .  c  o  m
    return q.getResultList();

}

From source file:com.expressui.sample.view.user.UserQuery.java

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

    if (!isEmpty(loginName)) {
        ParameterExpression<String> p = builder.parameter(String.class, "loginName");
        criteria.add(builder.like(builder.upper(rootEntity.<String>get("loginName")), p));
    }//from   w w  w .  ja va  2s .co m

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

    return criteria;
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public int itemsDateRangeCount(Date start, Date end) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.select(cb.count(rt.get(ActivityAIPC_.actinstproccodeser)));
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));

    Query q = em.createQuery(cq);
    return ((Long) (q.getSingleResult())).intValue();

}

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  om
    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:com.mxep.web.common.persistence.DynamicSpecifications.java

private static List<Predicate> getPredicate(Collection<SearchFilter> filters, Root<?> root,
        CriteriaQuery<?> query, CriteriaBuilder builder) {
    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]);
        }//from  ww  w.j av  a 2s  .  c o m

        /*????*/
        if (expression.getJavaType().equals(Integer.class)
                && !filter.operator.equals(SearchFilter.Operator.IN)) {
            filter.value = Integer.parseInt(filter.value.toString());
        }

        // logic operator
        switch (filter.operator) {
        case EQ:
            predicates.add(builder.equal(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;
        case NEQ:
            predicates.add(builder.notEqual(expression, filter.value));
            break;
        case IN:
            In in = builder.in(expression);
            String value = filter.value.toString();
            String[] values = StringUtils.split(value, ",");
            for (int i = 0; i < values.length; i++) {
                /*????*/
                if (expression.getJavaType().equals(Integer.class) || expression.getJavaType().equals(int.class)
                        || expression.getJavaType().equals(byte.class)) {
                    in.value(Integer.valueOf(values[i]));
                } else {
                    in.value(values[i]);
                }

            }
            predicates.add(in);
            break;
        case NULL:
            predicates.add(builder.isNull(expression));
            break;
        case NOTNULL:
            predicates.add(builder.isNotNull(expression));
            break;
        }
    }
    return predicates;
}

From source file:com.order.erp.web.controller.SystemController.java

@RequestMapping(path = "staff", method = RequestMethod.GET)
public String staffIndex(HttpServletRequest request,
        @RequestParam(name = "keyword", required = false) String keyword,
        @PageableDefault(page = 0, size = 10, sort = "id", direction = Direction.ASC) Pageable pageable,
        ModelMap model) {//from w w  w  .ja  va 2 s  . c  om
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    User me = staffService.findByAccountUsername(((UserDetails) principal).getUsername());
    Company company = me.getCompany();
    //List<User> userList = userService.findUsersByAmoebaId(company.getId());

    final User meStaff = me;
    final String finalKeyword = keyword;
    final Company finalCompany = company;
    Page<User> page = staffService.findUsers(new Specification<User>() {
        public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            query.distinct(true);
            Predicate p1 = cb.equal(root.get("company").as(Company.class), finalCompany);
            Predicate p2 = cb.conjunction();
            if (StringUtils.isNotBlank(finalKeyword)) {
                Predicate p1_like = cb.like(root.get("account").get("username").as(String.class),
                        "%" + finalKeyword + "%");
                Predicate p2_like = cb.like(root.get("realname").as(String.class), "%" + finalKeyword + "%");
                p2 = cb.or(p1_like, p2_like);
            }
            Predicate p3 = cb.notEqual(root.as(User.class), meStaff);
            Predicate p_result = cb.and(p1, p2, p3);
            return p_result;
        }
    }, pageable);

    model.put("user", me);
    model.put("company", company);
    model.put("page", page);
    return "/system/staff_list";
}

From source file:com.i5le.framwork.core.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//ww  w.j a  v  a  2 s.  co m
        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]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(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;

                    case NEQ:
                        predicates.add(builder.notEqual(expression, filter.value));
                        break;

                    }
                }

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

            return builder.conjunction();
        }
    };
}

From source file:com.frame.base.repository.jpa.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override//w  ww.  j a v  a2 s . c  o m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && !filters.isEmpty()) {
                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]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(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;
                    case NE:
                        predicates.add(builder.notEqual(expression, (Comparable) filter.value));
                    case IN:
                        predicates.add(
                                builder.in(expression).in(Arrays.asList(filter.value.toString().split(","))));
                    }
                }

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

            return builder.conjunction();
        }
    };
}

From source file:com.eu.evaluation.server.dao.AbstractDAO.java

public boolean isUnique(T entity, String... propertys) {
    if (propertys == null || propertys.length == 0) {
        return true;
    }//from  w  w  w . ja v a 2  s. co  m
    try {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass);
        Root<T> root = criteriaQuery.from(entityClass);
        Predicate predicate = null;
        for (String property : propertys) {
            if (predicate == null) {
                predicate = cb.equal(root.get(property), PropertyUtils.getProperty(entity, property));
            } else {
                predicate = cb.and(predicate,
                        cb.equal(root.get(property), PropertyUtils.getProperty(entity, property)));
            }
        }

        if (!StringUtils.isBlank(entity.getId())) {
            predicate = cb.and(predicate, cb.notEqual(root.get("id"), entity.getId()));
        }
        criteriaQuery.where(predicate);

        TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);
        List<T> result = typedQuery.getResultList();
        return result.isEmpty();
    } catch (Exception e) {
        e.printStackTrace();
        ReflectionUtils.handleReflectionException(e);
    }
    return false;
}

From source file:com.it.j2ee.modules.common.util.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        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]);
                    }/*from  ww  w  . j av a2s.co m*/

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(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;
                    case IN:
                        CriteriaBuilder.In in = builder.in(expression);
                        String values = (String) filter.value;
                        String[] value = values.split(",");
                        for (int i = 0; i < value.length; i++) {
                            in.value(value[i]);
                        }
                        predicates.add(in);
                        break;
                    case NEQ:
                        predicates.add(builder.notEqual(expression, (Comparable) filter.value));
                        break;
                    }
                }

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

            return builder.conjunction();
        }
    };
}