Example usage for javax.persistence.criteria CriteriaBuilder equal

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

Introduction

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

Prototype

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

Source Link

Document

Create a predicate for testing the arguments for equality.

Usage

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

public TBusApproveSugEntity getApplyApproveInfoByApplyNo(String applyNo) {
    if (applyNo == null) {
        throw new InvalidParameterException("Apply No can not be null!");
    }/*from  ww  w.  ja v  a2s . c  o  m*/
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class);
    Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class);
    approveRoot.fetch(TBusApproveSugEntity_.application);
    cq.where(cb.equal(approveRoot.get(TBusApproveSugEntity_.application).get(TBusApplyinfoEntity_.applyno),
            applyNo));
    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

From source file:com.carser.viamais.vo.TransactionFilter.java

private Predicate[] getPredicates(CriteriaBuilder cb, Root<Transaction> transaction) {
    List<Predicate> predicates = new ArrayList<>();
    if (StringUtils.hasLength(pattern)) {
        String[] words = pattern.split(" ");
        for (String word : words) {
            Predicate model = cb.like(transaction.get(Transaction_.car).get(Car_.model).get(Model_.name),
                    "%" + word + "%");
            Predicate customer = cb.like(transaction.get(Transaction_.customer).get(Customer_.name),
                    "%" + word + "%");
            predicates.add((cb.or(model, customer)));
        }//from   www.j  a v a  2  s  .  co  m
    }
    if (StringUtils.hasLength(rg)) {
        predicates.add(cb.like(transaction.get(Transaction_.customer).get(Customer_.rg), "%" + rg + "%"));
    }
    if (StringUtils.hasLength(cpf)) {
        predicates.add(cb.like(transaction.get(Transaction_.customer).get(Customer_.cpf).as(String.class),
                "%" + cpf + "%"));
    }
    if (StringUtils.hasLength(renavam)) {
        predicates.add(cb.like(transaction.get(Transaction_.car).get(Car_.renavam), "%" + renavam + "%"));
    }
    if (StringUtils.hasLength(plate)) {
        predicates.add(cb.equal(transaction.get(Transaction_.car).get(Car_.licensePlate), plate));
    }
    if (StringUtils.hasLength(chassi)) {
        predicates.add(cb.like(transaction.get(Transaction_.car).get(Car_.chassi), "%" + chassi + "%"));
    }
    Expression<Date> date = cb.function("date", Date.class, transaction.get(Transaction_.transactionDate));
    if (startDate != null) {
        predicates.add(cb.greaterThanOrEqualTo(date, startDate));
    }
    if (finalDate != null) {
        predicates.add(cb.lessThanOrEqualTo(date, finalDate));
    }
    if (StringUtils.hasLength(type)) {
        switch (type) {
        case "sale":
            predicates.add(cb.equal(transaction.get(Transaction_.type), Sale.class.getSimpleName()));
            break;
        case "purchase":
            predicates.add(cb.equal(transaction.get(Transaction_.type), Purchase.class.getSimpleName()));
            break;
        default:
            throw new BusinessException("transaction type not supported");
        }
    }
    if (StringUtils.hasLength(advertiser)) {
        predicates.add(cb.equal(transaction.get(Transaction_.advertiser), advertiser));
    }
    if (StringUtils.hasLength(financial)) {
        predicates.add(cb.like(cb.treat(transaction, Sale.class).get(Sale_.financial), financial));
    }
    if (seller != null) {
        predicates.add(cb.equal(transaction.get(Transaction_.seller).get(Seller_.id), seller));
    }
    return (predicates.toArray(new Predicate[predicates.size()]));
}

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

public TBusApplyinfoEntity getApplicationById(String id, PrincipalExt principalExt) {
    applySecurityFilter("applications", principalExt);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class);
    Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class);
    applyRoot.fetch(TBusApplyinfoEntity_.passenger, JoinType.LEFT);
    applyRoot.fetch(TBusApplyinfoEntity_.senduser, JoinType.LEFT);
    cq.where(cb.equal(applyRoot.get(TBusApplyinfoEntity_.uuid), id));
    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

private void addRestrictions(CriteriaQuery<T> criteriaQuery, Pageable pageable) {
    if (criteriaQuery == null || pageable == null) {
        return;/*from  ww w  .j a va2 s  .  c  o m*/
    }
    Root<T> root = getRoot(criteriaQuery);
    if (root == null) {
        return;
    }
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Predicate restrictions = criteriaQuery.getRestriction() != null ? criteriaQuery.getRestriction()
            : criteriaBuilder.conjunction();
    if (StringUtils.isNotEmpty(pageable.getSearchProperty())
            && StringUtils.isNotEmpty(pageable.getSearchValue())) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder
                .like(root.<String>get(pageable.getSearchProperty()), "%" + pageable.getSearchValue() + "%"));
    }
    if (pageable.getFilters() != null) {
        for (Filter filter : pageable.getFilters()) {
            if (filter == null || StringUtils.isEmpty(filter.getProperty())) {
                continue;
            }
            if (filter.getOperator() == Operator.eq && filter.getValue() != null) {
                if (filter.getIgnoreCase() != null && filter.getIgnoreCase()
                        && filter.getValue() instanceof String) {
                    restrictions = criteriaBuilder.and(restrictions,
                            criteriaBuilder.equal(criteriaBuilder.lower(root.<String>get(filter.getProperty())),
                                    ((String) filter.getValue()).toLowerCase()));
                } else {
                    restrictions = criteriaBuilder.and(restrictions,
                            criteriaBuilder.equal(root.get(filter.getProperty()), filter.getValue()));
                }
            } else if (filter.getOperator() == Operator.ne && filter.getValue() != null) {
                if (filter.getIgnoreCase() != null && filter.getIgnoreCase()
                        && filter.getValue() instanceof String) {
                    restrictions = criteriaBuilder.and(restrictions,
                            criteriaBuilder.notEqual(
                                    criteriaBuilder.lower(root.<String>get(filter.getProperty())),
                                    ((String) filter.getValue()).toLowerCase()));
                } else {
                    restrictions = criteriaBuilder.and(restrictions,
                            criteriaBuilder.notEqual(root.get(filter.getProperty()), filter.getValue()));
                }
            } else if (filter.getOperator() == Operator.gt && filter.getValue() != null) {
                restrictions = criteriaBuilder.and(restrictions,
                        criteriaBuilder.gt(root.<Number>get(filter.getProperty()), (Number) filter.getValue()));
            } else if (filter.getOperator() == Operator.lt && filter.getValue() != null) {
                restrictions = criteriaBuilder.and(restrictions,
                        criteriaBuilder.lt(root.<Number>get(filter.getProperty()), (Number) filter.getValue()));
            } else if (filter.getOperator() == Operator.ge && filter.getValue() != null) {
                restrictions = criteriaBuilder.and(restrictions,
                        criteriaBuilder.ge(root.<Number>get(filter.getProperty()), (Number) filter.getValue()));
            } else if (filter.getOperator() == Operator.le && filter.getValue() != null) {
                restrictions = criteriaBuilder.and(restrictions,
                        criteriaBuilder.le(root.<Number>get(filter.getProperty()), (Number) filter.getValue()));
            } else if (filter.getOperator() == Operator.like && filter.getValue() != null
                    && filter.getValue() instanceof String) {
                restrictions = criteriaBuilder.and(restrictions, criteriaBuilder
                        .like(root.<String>get(filter.getProperty()), (String) filter.getValue()));
            } else if (filter.getOperator() == Operator.in && filter.getValue() != null) {
                restrictions = criteriaBuilder.and(restrictions,
                        root.get(filter.getProperty()).in(filter.getValue()));
            } else if (filter.getOperator() == Operator.isNull) {
                restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).isNull());
            } else if (filter.getOperator() == Operator.isNotNull) {
                restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).isNotNull());
            }
        }
    }
    criteriaQuery.where(restrictions);
}

From source file:fi.vm.sade.eperusteet.ylops.service.ops.impl.OpetussuunnitelmaServiceImpl.java

private CriteriaQuery<Opetussuunnitelma> getQuery(OpetussuunnitelmaQuery pquery) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Opetussuunnitelma> query = builder.createQuery(Opetussuunnitelma.class);
    Root<Opetussuunnitelma> ops = query.from(Opetussuunnitelma.class);

    List<Predicate> ehdot = new ArrayList<>();

    // VAIN JULKAISTUT
    ehdot.add(builder.equal(ops.get(Opetussuunnitelma_.tila), Tila.JULKAISTU));

    // Haettu organisaatio lytyy opsilta
    if (pquery.getOrganisaatio() != null) {
        Expression<Set<String>> organisaatiot = ops.get(Opetussuunnitelma_.organisaatiot);
        ehdot.add(builder.and(builder.isMember(pquery.getOrganisaatio(), organisaatiot)));
    }//from  w  ww  .j  a v  a  2  s .c  o m

    // Koulutustyyppi
    if (pquery.getKoulutustyyppi() != null) {
        ehdot.add(builder.and(builder.equal(ops.get(Opetussuunnitelma_.koulutustyyppi),
                KoulutusTyyppi.of(pquery.getKoulutustyyppi()))));
    }

    // Perusteen tyyppi
    if (pquery.getTyyppi() != null) {
        ehdot.add(builder.and(builder.equal(ops.get(Opetussuunnitelma_.tyyppi), pquery.getTyyppi())));
    }

    // Perusteen id
    if (pquery.getPerusteenId() != null) {
        Path<PerusteCache> cachedPeruste = ops.join(Opetussuunnitelma_.cachedPeruste);
        ehdot.add(builder
                .and(builder.equal(cachedPeruste.get(PerusteCache_.perusteId), pquery.getPerusteenId())));
    }

    // Perusteen diaarinumero
    if (pquery.getPerusteenDiaarinumero() != null) {
        ehdot.add(builder.and(builder.equal(ops.get(Opetussuunnitelma_.perusteenDiaarinumero),
                pquery.getPerusteenDiaarinumero())));
    }

    query.where(ehdot.toArray(new Predicate[ehdot.size()]));

    return query.select(ops);
}

From source file:eu.domibus.common.dao.MessageLogDao.java

public List<MessageLogEntry> findPaged(int from, int max, String column, boolean asc,
        HashMap<String, Object> filters) {

    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<MessageLogEntry> cq = cb.createQuery(MessageLogEntry.class);
    Root<MessageLogEntry> mle = cq.from(MessageLogEntry.class);
    cq.select(mle);/*  w  w  w . jav a  2 s. c o m*/
    List<Predicate> predicates = new ArrayList<Predicate>();
    for (Map.Entry<String, Object> filter : filters.entrySet()) {
        if (filter.getValue() != null) {
            if (filter.getValue() instanceof String) {
                if (!filter.getValue().toString().isEmpty()) {
                    switch (filter.getKey().toString()) {
                    case "receivedFrom":
                        predicates.add(cb.greaterThanOrEqualTo(mle.<Date>get("received"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "receivedTo":
                        predicates.add(cb.lessThanOrEqualTo(mle.<Date>get("received"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    default:
                        predicates.add(cb.like(mle.<String>get(filter.getKey()), (String) filter.getValue()));
                        break;
                    }
                }
            } else {
                predicates.add(cb.equal(mle.<String>get(filter.getKey()), filter.getValue()));
            }
        }
    }
    cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    if (column != null) {
        if (asc) {
            cq.orderBy(cb.asc(mle.get(column)));
        } else {
            cq.orderBy(cb.desc(mle.get(column)));
        }

    }
    final TypedQuery<MessageLogEntry> query = this.em.createQuery(cq);
    query.setFirstResult(from);
    query.setMaxResults(max);
    return query.getResultList();
}

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;
        }//from   w  w w. j  a  va  2s. 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:com.yunguchang.data.ApplicationRepository.java

private TSysOrgEntity findRightFleetOfCoordinator(String coordinatorId) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TSysOrgEntity> cq = cb.createQuery(TSysOrgEntity.class);
    Root<TBusBusinessRelaEntity> mapRoot = cq.from(TBusBusinessRelaEntity.class);

    Root<TSysUserEntity> userRoot = cq.from(TSysUserEntity.class);

    cq.select(mapRoot.get(TBusBusinessRelaEntity_.fleet));

    Path<TSysOrgEntity> coordinatorOrg = userRoot.get(TSysUserEntity_.department);
    cq.where(cb.or(cb.and(cb.like(coordinatorOrg.get(TSysOrgEntity_.orgid), "001%"

    ), cb.equal(mapRoot.get(TBusBusinessRelaEntity_.fleet), coordinatorOrg)),
            cb.and(cb.notLike(coordinatorOrg.get(TSysOrgEntity_.orgid), "001%"),
                    cb.equal(mapRoot.get(TBusBusinessRelaEntity_.busOrg), coordinatorOrg))

    ),/*from  w  w  w.  j  av a 2s  . c o  m*/

            cb.equal(userRoot.get(TSysUserEntity_.userid), coordinatorId)

    );

    return Iterables.getFirst(em.createQuery(cq).getResultList(), null);
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> forFirmwareModuleVersion(final FirmwareModuleFilterType firmwareModuleFilterType,
        final String firmwareModuleVersion) throws ArgumentNullOrEmptyException {
    if (StringUtils.isEmpty(firmwareModuleVersion)) {
        throw new ArgumentNullOrEmptyException("firmwareModuleVersion");
    }/*from  w  w  w.  j av  a 2 s.  c  o m*/
    if (firmwareModuleFilterType == null) {
        throw new ArgumentNullOrEmptyException("firmwareModuleType");
    }

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {
            String moduleFieldName = "";
            switch (firmwareModuleFilterType) {
            case COMMUNICATION:
                moduleFieldName = "moduleVersionComm";
                break;
            case FUNCTIONAL:
                moduleFieldName = "moduleVersionFunc";
                break;
            case SECURITY:
                moduleFieldName = "moduleVersionSec";
                break;
            case M_BUS:
                moduleFieldName = "moduleVersionMbus";
                break;
            case MODULE_ACTIVE:
                moduleFieldName = "moduleVersionMa";
                break;
            case ACTIVE_FIRMWARE:
                break;

            default:
                break;
            }

            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceFirmware> deviceFirmwareRoot = subquery.from(DeviceFirmware.class);
            subquery.select(deviceFirmwareRoot.get("device").get("id").as(Long.class));
            subquery.where(cb.and(
                    cb.like(cb.upper(deviceFirmwareRoot.get("firmware").<String>get(moduleFieldName)),
                            firmwareModuleVersion.toUpperCase()),
                    cb.equal(deviceFirmwareRoot.<Boolean>get("active"), true)));
            return cb.in(deviceRoot.get("id").as(Long.class)).value(subquery);
        }
    };
}

From source file:com.movies.dao.impl.BaseDaoImpl.java

@Override
public <T> List<T> getObjectsByCriteria(Map<String, Object> map, Class returnClass,
        List<SingularAttribute> singleAttributes, List<ListAttribute> listAttributes,
        List<SetAttribute> setAttributes) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(returnClass).distinct(true);
    Root<T> root = cq.from(returnClass);

    if (CollectionUtils.isNotEmpty(singleAttributes)) {
        for (SingularAttribute attribute : singleAttributes) {
            root.join(attribute);//from  w  ww. j a v  a2  s. co  m
            root.fetch(attribute);
        }
    }

    if (CollectionUtils.isNotEmpty(listAttributes)) {
        for (ListAttribute attribute : listAttributes) {
            root.join(attribute, JoinType.LEFT);
            root.fetch(attribute, JoinType.LEFT);
        }
    }

    if (CollectionUtils.isNotEmpty(setAttributes)) {
        for (SetAttribute attribute : setAttributes) {
            root.join(attribute, JoinType.LEFT);
            root.fetch(attribute, JoinType.LEFT);
        }
    }
    Set<Entry<String, Object>> set = map.entrySet();
    int numberOfClauses = set.size();
    Predicate[] predicates = new Predicate[numberOfClauses];
    int i = 0;
    for (Entry<String, Object> entry : set) {
        String key = entry.getKey();
        if (MovieConstants.NAME_FIELD.equals(key) || MovieConstants.SURNAME_FIELD.equals(key)) {
            predicates[i++] = cb.like(cb.upper(root.<String>get(key)), LIKE + entry.getValue() + LIKE);
        } else if (MovieConstants.MOVIE_DIRECTOR_FIELD.equals(key)) {
            //predicates[i++] = cb.equal( ,entry.getValue());
        } else {
            predicates[i++] = cb.equal(root.get(key), entry.getValue());
        }
    }

    return em.createQuery(cq.select(root).where(predicates)).getResultList();

}