Example usage for javax.persistence.criteria CriteriaQuery from

List of usage examples for javax.persistence.criteria CriteriaQuery from

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery from.

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:com.hiperium.dao.common.generic.GenericDAO.java

/**
 * /* w  ww  .  j a va2s . co  m*/
 * @param entity
 * @param bypassCache
 * @return
 */
protected Long count(T entity, boolean bypassCache) {
    this.logger.debug("count - START");
    CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
    Root<T> root = criteriaQuery.from(this.entityClass);
    criteriaQuery.select(criteriaBuilder.count(root));
    // Create JPA Query
    Query query = this.entityManager.createQuery(criteriaQuery);
    if (bypassCache) {
        this.setBypassCahe(query);
    }
    // get the query results
    Long result = (Long) query.getSingleResult();
    this.logger.debug("count - END: " + result);
    return result;
}

From source file:org.querybyexample.jpa.GenericRepository.java

/**
 * Find and load a list of E instance.//www  .j  av a  2 s  .c om
 *
 * @param entity a sample entity whose non-null properties may be used as
 * search hints
 * @param searchParameters carries additional search information
 * @return the entities matching the search.
 */
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<E> find(E entity, SearchParameters sp) {
    if (sp.hasNamedQuery()) {
        return byNamedQueryUtil.findByNamedQuery(sp);
    }
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<E> criteriaQuery = builder.createQuery(type);
    if (sp.getDistinct()) {
        criteriaQuery.distinct(true);
    }
    Root<E> root = criteriaQuery.from(type);

    // predicate
    Predicate predicate = getPredicate(root, builder, entity, sp);
    if (predicate != null) {
        criteriaQuery = criteriaQuery.where(predicate);
    }

    // left join
    for (SingularAttribute<?, ?> arg : sp.getLeftJoins()) {
        root.fetch((SingularAttribute<E, ?>) arg, JoinType.LEFT);
    }

    // order by
    criteriaQuery.orderBy(orderByUtil.buildJpaOrders(sp.getOrders(), root, builder, sp));

    TypedQuery<E> typedQuery = entityManager.createQuery(criteriaQuery);
    applyCacheHints(typedQuery, sp);
    applyPagination(typedQuery, sp);
    List<E> entities = typedQuery.getResultList();
    log.debug("Returned {} elements", entities.size());

    return entities;
}

From source file:ch.puzzle.itc.mobiliar.business.resourcerelation.control.ResourceRelationService.java

public List<ConsumedResourceRelationEntity> getConsumedSlaveRelations(ResourceEntity slaveResource) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<ConsumedResourceRelationEntity> q = cb.createQuery(ConsumedResourceRelationEntity.class);
    Root<ConsumedResourceRelationEntity> r = q.from(ConsumedResourceRelationEntity.class);
    Join<ConsumedResourceRelationEntity, ResourceEntity> slaveResourceJoin = r.join("slaveResource");
    q.where(cb.equal(slaveResourceJoin.get("id"), slaveResource.getId()));

    TypedQuery<ConsumedResourceRelationEntity> query = entityManager.createQuery(q);
    return query.getResultList();
}

From source file:ch.puzzle.itc.mobiliar.business.resourcerelation.control.ResourceRelationService.java

public List<ProvidedResourceRelationEntity> getProvidedSlaveRelations(ResourceEntity slaveResource) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<ProvidedResourceRelationEntity> q = cb.createQuery(ProvidedResourceRelationEntity.class);
    Root<ProvidedResourceRelationEntity> r = q.from(ProvidedResourceRelationEntity.class);
    Join<ProvidedResourceRelationEntity, ResourceEntity> slaveResourceJoin = r.join("slaveResource");
    q.where(cb.equal(slaveResourceJoin.get("id"), slaveResource.getId()));

    TypedQuery<ProvidedResourceRelationEntity> query = entityManager.createQuery(q);
    return query.getResultList();
}

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()/*from  www .j  ava  2s. co  m*/
            .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

/**
 * Get the list of serviceIds available.
 *
 * @param modelVersion//  w w w. j a va  2s. c om
 *            the model version.
 * @return the list of serviceIds available.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<String> getRevisions() {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<String> q = cb.createQuery(String.class);
    final Root<EnterpriseType> f = q.from(EnterpriseType.class);
    q.select(f.<String>get(EnterpriseType_.modelVersion));
    q.orderBy(cb.asc(f.<String>get(EnterpriseType_.modelVersion)));
    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

/**
 * Get the list of {@link DataType} with the ids.
 *
 * @param modelVersion//  w  w  w. ja  va  2 s. c  o  m
 *            the model version.
 * @param ids
 *            the list of ids to return.
 * @return the list of {@link DataType}.
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<DataType> getDataTypesById(final String modelVersion, final List<String> ids) {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<DataType> q = cb.createQuery(DataType.class);
    final Root<DataType> f = q.from(DataType.class);
    final Predicate orIds = cb.disjunction();
    ids.stream().forEach(id -> orIds.getExpressions().add(cb.equal(f.<String>get(DataType_.id), id)));
    q.where(cb.equal(f.<String>get(DataType_.modelVersion), modelVersion), orIds);
    q.orderBy(cb.asc(f.<String>get(DataType_.name)), cb.asc(f.<String>get(DataType_.id)));
    final TypedQuery<DataType> typedQuery = this.em.createQuery(q);
    final List<DataType> value = typedQuery.getResultList();
    value.stream().forEach(dt -> EagerLoader.load(dt));
    return value;
}

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

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

    q.select(f.<String>get(FlowProcessType_.id));
    q.where(cb.equal(f.<String>get(FlowProcessType_.modelVersion), modelVersion),
            cb.equal(f.<String>get(FlowProcessType_.parentId), flowId));
    q.orderBy(cb.asc(f.<Long>get(FlowProcessType_.hjid)));
    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

/**
 * Get the last model version (lexical compare).
 *
 * @return the last model version.// w  w  w  .  ja v  a2s  .  c o  m
 * @since 3.5.1
 */
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public String getLastModelVersion() {
    String value = null;
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<String> q = cb.createQuery(String.class);
    final Root<EnterpriseType> f = q.from(EnterpriseType.class);
    q.select(f.<String>get(EnterpriseType_.modelVersion));
    q.orderBy(cb.desc(f.<String>get(EnterpriseType_.modelVersion)));
    final TypedQuery<String> typedQuery = this.em.createQuery(q);
    final List<String> list = typedQuery.getResultList();
    if (Objects.nonNull(list) && list.size() > 0) {
        value = list.get(0);
    }
    return value;
}

From source file:sf.net.experimaestro.scheduler.Resource.java

/**
 * Get a resource by locator/* w ww . j a  v a 2 s  . c  o  m*/
 *
 * @param em   The current entity manager
 * @param path The path of the resource
 * @return The resource or null if there is no such resource
 */
public static Resource getByLocator(EntityManager em, String path) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Resource> cq = cb.createQuery(Resource.class);
    Root<Resource> root = cq.from(Resource.class);
    cq.where(root.get("locator").in(cb.parameter(String.class, "locator")));

    TypedQuery<Resource> query = em.createQuery(cq);
    query.setParameter("locator", path);
    List<Resource> result = query.getResultList();
    assert result.size() <= 1;

    if (result.isEmpty())
        return null;

    return result.get(0);
}