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.qpark.eip.core.model.analysis.AnalysisDao.java

private List<String> getInterfaceMappingFieldDefinitionIds(final String modelVersion,
        final List<String> interfaceMappingIds) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<String> q = cb.createQuery(String.class);
    final Root<FieldType> f = q.from(FieldType.class);

    final Predicate orParentIds = cb.disjunction();
    interfaceMappingIds.stream()/*ww  w .j  a va2s.  c  om*/
            .forEach(id -> orParentIds.getExpressions().add(cb.equal(f.<String>get(FieldType_.parentId), id)));

    q.select(f.<String>get(FieldType_.fieldTypeDefinitionId));
    q.where(cb.equal(f.<String>get(FieldType_.modelVersion), modelVersion), orParentIds);
    final TypedQuery<String> typedQuery = this.em.createQuery(q);
    final List<String> value = typedQuery.getResultList();
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

private List<String> getFlowMappingInterfaceMappingTypeIds(final String modelVersion,
        final List<String> flowProcessTypeIds) {
    final List<String> value = new ArrayList<String>();
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<Long> q = cb.createQuery(Long.class);
    final Root<FlowMapInOutType> f = q.from(FlowMapInOutType.class);

    final Predicate orParentIds = cb.disjunction();
    flowProcessTypeIds.stream().forEach(
            id -> orParentIds.getExpressions().add(cb.equal(f.<String>get(FlowMapInOutType_.parentId), id)));

    q.select(f.<Long>get(FlowMapInOutType_.hjid));
    q.where(cb.equal(f.<String>get(FlowMapInOutType_.modelVersion), modelVersion), orParentIds);

    final TypedQuery<Long> typedQuery = this.em.createQuery(q);
    typedQuery.getResultList().stream().forEach(hjid -> {
        value.addAll(this.em.getReference(FlowMapInOutType.class, hjid).getInterfaceMappingId());
    });//from w w  w  .  ja  v  a 2s . com

    return value;
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

@Override
public List<T> findByAttributes(Map<SingularAttribute<T, ?>, Object> theAttributes) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());
    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> aPredicateList = new ArrayList<Predicate>();
    Iterator<SingularAttribute<T, ?>> anIterator = theAttributes.keySet().iterator();
    while (anIterator.hasNext()) {
        SingularAttribute<T, ?> aKey = anIterator.next();
        Object aValue = theAttributes.get(aKey);
        Predicate aPredicate = queryBuilder.equal(rootQuery.get(aKey), aValue);
        aPredicateList.add(aPredicate);//  w  w  w .j  a va 2 s.c  o  m
    }

    select.where(aPredicateList.toArray(new Predicate[0]));
    TypedQuery<T> typedQuery = entityManager.createQuery(select);

    return typedQuery.getResultList();
}

From source file:de.tudarmstadt.ukp.csniper.webapp.security.dao.AbstractDao.java

/**
 * Finds all entities that have the same type as the given example and all fields are equal to
 * non-null fields in the example./*from  w  w w  .  j a  va 2  s .  c o m*/
 */
protected <TT> CriteriaQuery<TT> queryByExample(TT aExample, String aOrderBy, boolean aAscending) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    @SuppressWarnings("unchecked")
    CriteriaQuery<TT> query = cb.createQuery((Class<TT>) aExample.getClass());
    @SuppressWarnings("unchecked")
    Root<TT> root = query.from((Class<TT>) aExample.getClass());
    query.select(root);

    List<Predicate> predicates = new ArrayList<Predicate>();
    BeanWrapper a = PropertyAccessorFactory.forBeanPropertyAccess(aExample);

    // Iterate over all properties
    for (PropertyDescriptor d : a.getPropertyDescriptors()) {
        Object value = a.getPropertyValue(d.getName());

        // Only consider writeable properties. This filters out e.g. the "class" (getClass())
        // property.
        if (value != null && a.isWritableProperty(d.getName())) {
            predicates.add(cb.equal(root.get(d.getName()), value));
        }
    }

    if (!predicates.isEmpty()) {
        query.where(predicates.toArray(new Predicate[predicates.size()]));
    }

    if (aOrderBy != null) {
        if (aAscending) {
            query.orderBy(cb.asc(root.get(aOrderBy)));
        } else {
            query.orderBy(cb.desc(root.get(aOrderBy)));
        }
    }

    return query;
}

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

@SuppressWarnings("unchecked")
public <T extends Identifiable<?>, M2O extends Identifiable<?>> List<Predicate> byExampleOnXToOne(
        ManagedType<T> mt, Root<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {
    List<Predicate> predicates = newArrayList();
    for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
        if (attr.getPersistentAttributeType() == MANY_TO_ONE
                || attr.getPersistentAttributeType() == ONE_TO_ONE) {
            M2O m2oValue = (M2O) jpaUtil.getValue(mtValue, mt.getAttribute(attr.getName()));
            Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType();
            Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr);
            ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType);
            if (m2oValue != null) {
                if (m2oValue.isIdSet()) { // we have an id, let's restrict only on this field
                    predicates.add(builder.equal(m2oPath.get("id"), m2oValue.getId()));
                } else {
                    predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder));
                }/*ww w .j a  va  2 s. c  o  m*/
            }
        }
    }
    return predicates;
}

From source file:dtu.ds.warnme.dao.impl.EventsDaoImpl.java

@Override
public List<EventEntity> getNearestEvents(Float nLat, Float sLat, Float eLng, Float wLng,
        EventType... eventTypes) {//from  w  w  w .  j  ava  2 s  .co  m
    CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
    CriteriaQuery<EventEntity> criteriaQuery = criteriaBuilder.createQuery(EventEntity.class);
    Root<EventEntity> root = criteriaQuery.from(EventEntity.class);

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.latitude), nLat));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.latitude), sLat));
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.longitude), eLng));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.longitude), wLng));

    if (ArrayUtils.isNotEmpty(eventTypes)) {
        List<Predicate> eventTypePredicates = new ArrayList<Predicate>();
        for (EventType et : eventTypes) {
            eventTypePredicates.add(criteriaBuilder.equal(root.get(EventEntity_.eventType), et));
        }
        predicates.add(criteriaBuilder.and(
                criteriaBuilder.or(eventTypePredicates.toArray(new Predicate[eventTypePredicates.size()]))));
    }

    criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
    return getAllByCriteria(criteriaQuery);
}

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.qpark.eip.core.model.analysis.AnalysisDao.java

/**
 * Get the list of {@link InterfaceMappingType}s of the flow with id.
 *
 * @param modelVersion/*from w  ww. ja  v a2s  . c om*/
 *            the model version.
 * @param flowId
 *            the flow id.
 * @return the list of {@link InterfaceMappingType}s of the flow with id.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<InterfaceMappingType> getFlowInterfaceMappingTypes(final String modelVersion, final String flowId) {
    final List<InterfaceMappingType> value = new ArrayList<InterfaceMappingType>();
    final List<String> flowProcessTypeIds = this.getFlowProcessTypeIds(modelVersion, flowId);
    final List<String> flowMappingInterfaceMappingTypeIds = this
            .getFlowMappingInterfaceMappingTypeIds(modelVersion, flowProcessTypeIds);
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<InterfaceMappingType> q = cb.createQuery(InterfaceMappingType.class);
    final Root<InterfaceMappingType> f = q.from(InterfaceMappingType.class);

    final Predicate orIds = cb.disjunction();
    flowMappingInterfaceMappingTypeIds.stream()
            .forEach(id -> orIds.getExpressions().add(cb.equal(f.<String>get(InterfaceMappingType_.id), id)));

    q.where(cb.equal(f.<String>get(InterfaceMappingType_.modelVersion), modelVersion), orIds);

    final TypedQuery<InterfaceMappingType> typedQuery = this.em.createQuery(q);
    value.addAll(typedQuery.getResultList());

    final List<String> interfaceMappingIds = value.stream().map(i -> i.getId()).collect(Collectors.toList());
    this.getInterfaceMappingInheritents(modelVersion, interfaceMappingIds, value);
    value.stream().forEach(im -> EagerLoader.load(im));
    return value;
}

From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java

private void getInterfaceMappingInheritents(final String modelVersion, final List<String> interfaceMappingIds,
        final List<InterfaceMappingType> interfaceMappings) {
    if (interfaceMappingIds.size() > 0) {
        List<InterfaceMappingType> value = new ArrayList<InterfaceMappingType>();
        final List<String> fieldDefinitionIds = this.getInterfaceMappingFieldDefinitionIds(modelVersion,
                interfaceMappingIds);/*from w ww  .j av  a2  s. com*/

        final CriteriaBuilder cb = this.em.getCriteriaBuilder();
        final CriteriaQuery<InterfaceMappingType> q = cb.createQuery(InterfaceMappingType.class);
        final Root<InterfaceMappingType> f = q.from(InterfaceMappingType.class);

        final Predicate orIds = cb.disjunction();
        fieldDefinitionIds.stream().forEach(
                id -> orIds.getExpressions().add(cb.equal(f.<String>get(InterfaceMappingType_.id), id)));

        q.where(cb.equal(f.<String>get(InterfaceMappingType_.modelVersion), modelVersion), orIds);

        final TypedQuery<InterfaceMappingType> typedQuery = this.em.createQuery(q);
        value = typedQuery.getResultList();
        interfaceMappings.addAll(value);
        final List<String> foundIds = value.stream().map(i -> i.getId()).collect(Collectors.toList());
        this.getInterfaceMappingInheritents(modelVersion, foundIds, interfaceMappings);
    }
}

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

public List<Product> findList(ProductCategory productCategory, Date beginDate, Date endDate, Integer first,
        Integer count) {/*ww w.ja  v  a 2  s  .  c  om*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isMarketable"), true));
    if (productCategory != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.or(criteriaBuilder.equal(root.get("productCategory"), productCategory),
                        criteriaBuilder.like(root.get("productCategory").<String>get("treePath"),
                                "%" + ProductCategory.TREE_PATH_SEPARATOR + productCategory.getId()
                                        + ProductCategory.TREE_PATH_SEPARATOR + "%")));
    }
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate));
    }
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate));
    }
    criteriaQuery.where(restrictions);
    criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")));
    return super.findList(criteriaQuery, first, count, null, null);
}