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.sfs.ucm.controller.SpecificationAction.java

/**
 * load specifications/*w  w  w  . j ava 2s.c om*/
 */
private void loadList() throws UCMException {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Specification> c = cb.createQuery(Specification.class);
    Root<Specification> obj = c.from(Specification.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.specifications = em.createQuery(c).getResultList();

}

From source file:org.jdal.dao.jpa.JpaDao.java

/**
 * {@inheritDoc}/*from w  w w. j a  va 2s .  c o m*/
 */
public <E> List<E> getAll(Class<E> clazz) {
    CriteriaQuery<E> q = em.getCriteriaBuilder().createQuery(clazz);
    q.from(clazz);

    return em.createQuery(q).getResultList();
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * find tests associated with this artifact
 *///  w ww.  ja v a2s  .co m
private List<SpecificationTest> findSpecificationTests(Specification specification) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SpecificationTest> c = cb.createQuery(SpecificationTest.class);
    Root<SpecificationTest> obj = c.from(SpecificationTest.class);
    c.select(obj).where(cb.equal(obj.get("specification"), specification));
    return em.createQuery(c).getResultList();
}

From source file:dk.dma.msinm.service.AreaService.java

/**
 * Looks up an area by name//  w w w.j av  a  2  s  .  c om
 * @param name the name to search for
 * @param lang the language. Optional
 * @param parentId the parent ID. Optional
 * @return The matching area, or null if not found
 */
public Area findByName(String name, String lang, Integer parentId) {
    // Sanity check
    if (StringUtils.isBlank(name)) {
        return null;
    }

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Area> areaQuery = builder.createQuery(Area.class);

    Root<Area> areaRoot = areaQuery.from(Area.class);

    // Build the predicate
    PredicateHelper<Area> predicateBuilder = new PredicateHelper<>(builder, areaQuery);

    // Match the name
    Join<Area, AreaDesc> descs = areaRoot.join("descs", JoinType.LEFT);
    predicateBuilder.like(descs.get("name"), name);
    // Optionally, match the language
    if (StringUtils.isNotBlank(lang)) {
        predicateBuilder.equals(descs.get("lang"), lang);
    }

    // Optionally, match the parent
    if (parentId != null) {
        areaRoot.join("parent", JoinType.LEFT);
        Path<Area> parent = areaRoot.get("parent");
        predicateBuilder.equals(parent.get("id"), parentId);
    }

    // Complete the query
    areaQuery.select(areaRoot).distinct(true).where(predicateBuilder.where());

    // Execute the query and update the search result
    List<Area> result = em.createQuery(areaQuery).getResultList();

    return result.size() > 0 ? result.get(0) : null;
}

From source file:org.jdal.dao.jpa.JpaDao.java

/**
 * {@inheritDoc}/*w  w w .  j  a v  a 2  s . c om*/
 */
public List<T> getAll() {
    CriteriaQuery<T> q = em.getCriteriaBuilder().createQuery(getEntityClass());
    q.from(getEntityClass());

    return em.createQuery(q).getResultList();
}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * find tests associated with this artifact
 *//*from  w  ww .j  a  v  a2 s .  co  m*/
private List<SpecificationRuleTest> findSpecificationRuleTests(SpecificationRule specificationRule) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SpecificationRuleTest> c = cb.createQuery(SpecificationRuleTest.class);
    Root<SpecificationRuleTest> obj = c.from(SpecificationRuleTest.class);
    c.select(obj).where(cb.equal(obj.get("specificationRule"), specificationRule));
    return em.createQuery(c).getResultList();
}

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

/**
 * /*from  ww  w . ja v a  2  s.  c  o  m*/
 * @param bypassCache
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected List<T> findAll(boolean bypassCache) {
    CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
    CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
    criteriaQuery.select(criteriaQuery.from(this.entityClass));
    Query query = this.entityManager.createQuery(criteriaQuery);
    if (bypassCache) {
        this.setBypassCahe(query);
    }
    return query.getResultList();
}

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

protected Long count(CriteriaQuery<T> criteriaQuery, List<Filter> filters) {
    Assert.notNull(criteriaQuery);// w  ww .j a v  a 2  s .c o  m
    Assert.notNull(criteriaQuery.getSelection());
    Assert.notEmpty(criteriaQuery.getRoots());

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    addRestrictions(criteriaQuery, filters);

    CriteriaQuery<Long> countCriteriaQuery = criteriaBuilder.createQuery(Long.class);
    for (Root<?> root : criteriaQuery.getRoots()) {
        Root<?> dest = countCriteriaQuery.from(root.getJavaType());
        dest.alias(getAlias(root));
        copyJoins(root, dest);
    }

    Root<?> countRoot = getRoot(countCriteriaQuery, criteriaQuery.getResultType());
    countCriteriaQuery.select(criteriaBuilder.count(countRoot));

    if (criteriaQuery.getGroupList() != null) {
        countCriteriaQuery.groupBy(criteriaQuery.getGroupList());
    }
    if (criteriaQuery.getGroupRestriction() != null) {
        countCriteriaQuery.having(criteriaQuery.getGroupRestriction());
    }
    if (criteriaQuery.getRestriction() != null) {
        countCriteriaQuery.where(criteriaQuery.getRestriction());
    }
    return entityManager.createQuery(countCriteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult();
}

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

/**
 * Get the list of all {@link EnterpriseType}s.
 *
 * @return the list of all {@link EnterpriseType}s.
 *///w w w  .j av  a 2 s. com
@Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<EnterpriseType> getEnterprises() {
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<EnterpriseType> q = cb.createQuery(EnterpriseType.class);
    q.from(EnterpriseType.class);
    final TypedQuery<EnterpriseType> typedQuery = this.em.createQuery(q);
    final List<EnterpriseType> value = typedQuery.getResultList();
    return value;
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.SessionDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllSessions = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<SessionImpl>>() {
                @Override//  www .  j  a va2 s .  c  o m
                public CriteriaQuery<SessionImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<SessionImpl> criteriaQuery = cb.createQuery(SessionImpl.class);
                    final Root<SessionImpl> definitionRoot = criteriaQuery.from(SessionImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });
}