Example usage for javax.persistence.criteria CriteriaBuilder createQuery

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

Introduction

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

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

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

/**
 * Get the {@link FieldMappingType} with the id.
 *
 * @param modelVersion/*from   w  w  w .  j a  v a2s.co  m*/
 *            the model version.
 * @param id
 *            the id to return.
 * @return the {@link FieldMappingType}.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public Optional<FieldMappingType> getFieldMappingTypeById(final String modelVersion, final String id) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<FieldMappingType> q = cb.createQuery(FieldMappingType.class);
    final Root<FieldMappingType> f = q.from(FieldMappingType.class);
    q.where(cb.equal(f.<String>get(FieldMappingType_.modelVersion), modelVersion),
            cb.equal(f.<String>get(FieldMappingType_.id), id));
    final TypedQuery<FieldMappingType> typedQuery = this.em.createQuery(q);
    final List<FieldMappingType> list = typedQuery.getResultList();

    final Optional<FieldMappingType> value = list.stream().findFirst();
    value.ifPresent(ct -> EagerLoader.load(ct));
    return value;
}

From source file:com.sfs.captor.controller.AlternativeFlowAction.java

/**
 * check for duplicate alternative flow//  w  w w .j ava  2  s  . c  o m
 */
private boolean checkForDuplicateBusinessRule(String name) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<FlowStepRule> c = cb.createQuery(FlowStepRule.class);
    Root<FlowStepRule> obj = c.from(FlowStepRule.class);
    c.select(obj).where(cb.equal(obj.get("name"), name));
    List<FlowStepRule> list = em.createQuery(c).getResultList();
    return (list.size() > 0);
}

From source file:com.sfs.captor.controller.AlternativeFlowAction.java

/**
 * Find testcases associated with this alternative flow
 * <p>//from ww  w  . java  2 s  .c o m
 * Return all testcases where flow name is contained in the testcase name.
 * 
 * @param alternativeFlow
 * @return List of TestCase
 */
private List<TestCase> findTestCases(Flow alternativeFlow) {
    List<TestCase> testCases = new ArrayList<TestCase>();

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<TestCase> c = cb.createQuery(TestCase.class);
    Root<TestCase> obj = c.from(TestCase.class);
    c.select(obj);
    List<TestCase> list = em.createQuery(c).getResultList();

    for (TestCase testCase : list) {

        if (testCase.getName().indexOf(alternativeFlow.getName()) != -1) {
            testCases.add(testCase);
        }
    }

    return testCases;
}

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/* ww w .  j a  v a  2s .  c  o m*/
 *            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.uni.dao.etc.UniJpaRepository.java

/**
 * Creates a new count query for the given {@link Specification}.
 * //from  w w w .j av a 2s  .  c o m
 * @param spec can be {@literal null}.
 * @return
 */
private TypedQuery<Long> getCountQuery(Specification<T> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<T> root = applySpecificationToCriteria(spec, query);
    query.select(builder.count(root));

    return em.createQuery(query);
}

From source file:com.uni.dao.etc.UniJpaRepository.java

/**
 * Creates a {@link TypedQuery} for the given {@link Specification} and {@link Sort}.
 * /*from   w  ww . j  a  v  a2 s . co m*/
 * @param spec can be {@literal null}.
 * @param sort can be {@literal null}.
 * @return
 */
private TypedQuery<T> getQuery(Specification<T> spec, Sort sort) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(getDomainClass());

    Root<T> root = applySpecificationToCriteria(spec, query);
    query.select(root);

    if (sort != null) {
        query.orderBy(toOrders(sort, root, builder));
    }

    return applyLockMode(em.createQuery(query));
}

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   ww w . j  a  v a2s.c  o m

        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:com.hiperium.dao.common.generic.GenericDAO.java

/**
 * Sets the criteria parameters for searching execution.
 *
 * @param entity/*from www.j a v a 2 s .  c o  m*/
 *            the entity with the values for the criteria.
 * @param fieldsToSort
 *            the fields to sort the result.
 */
private <E> CriteriaQuery<E> configureCriteria(E entity, Class<E> entityClass, String... fieldsToSort) {
    CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(entityClass);
    Root<E> root = criteriaQuery.from(entityClass);
    criteriaQuery.select(root);
    this.constructQuery(criteriaBuilder, criteriaQuery, root, entity);
    Order[] orderCriteriaList = null;
    if (fieldsToSort.length > 0) {
        orderCriteriaList = new Order[fieldsToSort.length];
        for (int i = 0; i < fieldsToSort.length; i++) {
            if (fieldsToSort[i].startsWith("A,")) {
                if (fieldsToSort[i].contains(".")) {
                    String compositeValue = fieldsToSort[i].substring(2);
                    String compositeName = compositeValue.split("\\.")[0];
                    String compositeFieldName = compositeValue.split("\\.")[1];
                    orderCriteriaList[i] = criteriaBuilder.asc(root.get(compositeName).get(compositeFieldName));
                } else {
                    orderCriteriaList[i] = criteriaBuilder.asc(root.get(fieldsToSort[i].substring(2)));
                }
            } else if (fieldsToSort[i].startsWith("D,")) {
                if (fieldsToSort[i].contains(".")) {
                    String compositeValue = fieldsToSort[i].substring(2);
                    String compositeName = compositeValue.split("\\.")[0];
                    String compositeFieldName = compositeValue.split("\\.")[1];
                    orderCriteriaList[i] = criteriaBuilder
                            .desc(root.get(compositeName).get(compositeFieldName));
                } else {
                    orderCriteriaList[i] = criteriaBuilder.desc(root.get(fieldsToSort[i].substring(2)));
                }
            }
        }
    } else {
        List<String> ids = this.getIdFields(entity);
        orderCriteriaList = new Order[ids.size()];
        int i = 0;
        for (String id : ids) {
            if (id.startsWith(PK_OBJECT_NAME)) {
                String compositeFieldName = id.replace(PK_OBJECT_NAME.concat("."), "");
                orderCriteriaList[i] = criteriaBuilder.asc(root.get(PK_OBJECT_NAME).get(compositeFieldName));
            } else {
                orderCriteriaList[i] = criteriaBuilder.asc(root.get(id));
            }
            i = i + 1;
        }
    }
    criteriaQuery.orderBy(orderCriteriaList);
    return criteriaQuery;
}

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

public Page<Product> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Product>(Collections.<Product>emptyList(), 0, pageable);
    }// w  ww . jav a2  s  .com
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class);
    Root<Product> root = criteriaQuery.from(Product.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.join("favoriteMembers"), member));
    return super.findPage(criteriaQuery, pageable);
}

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);/*from   w  w w.  j a v  a 2 s.c  o  m*/
    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();
}