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:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute//from w w w.  ja v  a 2  s  .  c  om
public long countByFullTexts(final String terms) {
    LOGGER.debug("counting by text {} terms {}", persistentClass, terms);
    if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) {
        return 0;
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> q = cb.createQuery(Long.class);

    Root<T> p = q.from(persistentClass);
    final Expression<Long> count = newCountExpression(cb, p);
    q.select(count);
    q.where(newFullTextPredicate(cb, p, terms));

    try {
        return entityManager.createQuery(q).getSingleResult();
    } catch (NoResultException e) {
        return 0;
    }
}

From source file:dk.dma.msinm.common.settings.Settings.java

/**
 * Returns all settings from the database
 * @return all settings from the database
 *///from  w  ww. j  a v a  2s  .c om
public List<SettingsEntity> getAll() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SettingsEntity> cq = cb.createQuery(SettingsEntity.class);
    cq.from(SettingsEntity.class);
    return em.createQuery(cq).getResultList();
}

From source file:ca.uhn.fhir.jpa.dao.BaseHapiFhirSystemDao.java

@Override
public Map<String, Long> getResourceCounts() {
    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = builder.createTupleQuery();
    Root<?> from = cq.from(ResourceTable.class);
    cq.multiselect(from.get("myResourceType").as(String.class),
            builder.count(from.get("myResourceType")).as(Long.class));
    cq.groupBy(from.get("myResourceType"));

    TypedQuery<Tuple> q = myEntityManager.createQuery(cq);

    Map<String, Long> retVal = new HashMap<String, Long>();
    for (Tuple next : q.getResultList()) {
        String resourceName = next.get(0, String.class);
        Long count = next.get(1, Long.class);
        retVal.put(resourceName, count);
    }//w w w  .  j a  v a  2  s.  c o m
    return retVal;
}

From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java

private Query applicationServersForSuggestBox(String input) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> q = cb.createQuery(String.class);
    Root<ResourceEntity> r = q.from(ResourceEntity.class);
    Join<ResourceEntity, ResourceTypeEntity> resType = r.join("resourceType");
    Predicate appServerNamePred = cb.like(resType.<String>get("name"),
            DefaultResourceTypeDefinition.APPLICATIONSERVER.name());
    input = input + "%";
    q.where(cb.and(appServerNamePred, cb.like(r.<String>get("name"), input)));
    q.select(r.<String>get("name"));
    q.distinct(true);//ww w  . j  ava 2  s.c o m

    q.orderBy(cb.asc(r.get("name")));

    return entityManager.createQuery(q);
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute// w w w  .j  ava 2 s . co m
public List<T> findByAttribute(String attributeName, Object attributeValue, long first, long count,
        String... fg) {
    LOGGER.debug("Searching {} bu attribute {} : {}, fg {}",
            new Object[] { persistentClass, attributeName, attributeValue, fg });

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(cb.equal(p.get(attributeName), attributeValue));

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;

}

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

public Long count(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.select(cb.countDistinct((transaction)));
    cq.where(getPredicates(cb, transaction));
    return em.createQuery(cq).getSingleResult();
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private List<Product> getProducts_Mercifully() {
    return transactionTemplate.execute(new TransactionCallback<List<Product>>() {
        @Override/*from  ww  w.  j a  v  a  2  s .  co  m*/
        public List<Product> doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Image> imageRoot = query.from(Image.class);
            Join<Image, Product> productJoin = imageRoot.join(Image_.product);
            query.select(productJoin);
            query.distinct(true);
            List<Predicate> criteria = new ArrayList<Predicate>();
            criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%"));
            criteria.add(cb.gt(imageRoot.get(Image_.index), 0));
            query.where(cb.and(criteria.toArray(new Predicate[criteria.size()])));
            return entityManager.createQuery(query).getResultList();
        }
    });
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute//  w ww.  j  a  va 2 s .  c  o  m
public List<T> findByExample(final T example, final T example2, final OrderBy<T> sortParam, final long first,
        final long count, final String... fg) {
    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    OpenJPACriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> from = q.from(persistentClass);
    q.where(newQbePredicates(cb, from, example, example2));
    if (sortParam != null) {
        List<Order> orderList = sortParam.orders(cb, from);
        if (orderList != null && !orderList.isEmpty()) {
            q.orderBy(orderList);
        }
    }

    TypedQuery<T> query = entityManager.createQuery(q);
    OrmUtils.firstCount(query, first, count);

    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }
    return resultList;
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private Product getProduct_Mercilessly() {
    return transactionTemplate.execute(new TransactionCallback<Product>() {
        @Override/*from   w w w.  ja  v  a  2s  . co  m*/
        public Product doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> productRoot = query.from(Product.class);

            query.select(productRoot)
                    .where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"), cb.gt(
                            productRoot.get(Product_.warehouseProductInfo).get(WarehouseProductInfo_.quantity),
                            50)));
            return entityManager.createQuery(query).getSingleResult();
        }
    });
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private Product getProduct_Mercifully() {
    return transactionTemplate.execute(new TransactionCallback<Product>() {
        @Override// w  w w  .  j  a  v a2  s  .  co  m
        public Product doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> productRoot = query.from(Product.class);
            Join<Product, WarehouseProductInfo> warehouseProductInfoJoin = productRoot
                    .join(Product_.warehouseProductInfo);

            query.select(productRoot).where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"),
                    cb.gt(warehouseProductInfoJoin.get(WarehouseProductInfo_.quantity), 50)));
            return entityManager.createQuery(query).getSingleResult();
        }
    });
}