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:eu.uqasar.service.company.CompanyService.java

public long countAllFiltered(final CompanyFilterStructure filter) {
    logger.infof("counting all Companies matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Company> from = criteria.from(Company.class);
    criteria.where(cb.or(cb.equal(from.get(Company_.name), filter.getName()),
            cb.equal(from.get(Company_.shortName), filter.getShortName()),
            cb.equal(from.get(Company_.country), filter.getCountry())));
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}

From source file:com.zergiu.tvman.dao.impl.TVShowDaoImpl.java

@Override
public Long getActiveTVShowsCount() {
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = criteria.createQuery(Long.class);
    Root<TVShow> show = query.from(TVShow.class);
    query.select(criteria.countDistinct(show));
    query.where(criteria.equal(show.<TVShowStatus>get("status"), TVShowStatus.Continuing));
    Long count = em.createQuery(query).getSingleResult();
    log.debug("active count: " + count);
    return count;
}

From source file:org.wallride.repository.CategoryRepositoryImpl.java

@Override
public void lock(long id) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<Category> root = query.from(Category.class);
    query.select(root.get(Category_.id));
    query.where(cb.equal(root.get(Category_.id), id));
    entityManager.createQuery(query).setLockMode(LockModeType.PESSIMISTIC_WRITE).getSingleResult();
}

From source file:de.ks.idnadrev.information.view.InformationOverviewDS.java

protected List<Tag> getTags(List<String> tagNames, EntityManager em) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tag> query = builder.createQuery(Tag.class);
    Root<Tag> root = query.from(Tag.class);
    Path<String> namePath = root.get(KEY_NAME);
    query.select(root);//ww w  .ja  v a2  s  . c om
    query.where(namePath.in(tagNames));

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

From source file:eu.uqasar.service.company.CompanyService.java

/**
 * //from  w  w w.  j a v  a 2 s . c  om
 * @param innovationObjective
 * @return
 */
public List<Company> getAllByAscendingName() {
    logger.infof("loading all Company ordered by ascending name ...");
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Company> criteria = cb.createQuery(Company.class);
    Root<Company> root = criteria.from(Company.class);
    criteria.orderBy(cb.asc(root.get(Company_.name)));
    return em.createQuery(criteria).getResultList();
}

From source file:com.boxedfolder.carrot.repository.impl.AnalyticsLogRepositoryImpl.java

private TypedQuery<Long> getCountQuery(Class clazz) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    query.select(builder.count(query.from(clazz)));
    return entityManager.createQuery(query);
}

From source file:br.ufba.dcc.mestrado.computacao.repository.impl.LicenseRepositoryImpl.java

@Override
public OpenHubLicenseEntity findByName(String name) {
    CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<OpenHubLicenseEntity> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());

    Root<OpenHubLicenseEntity> root = criteriaQuery.from(getEntityClass());
    CriteriaQuery<OpenHubLicenseEntity> select = criteriaQuery.select(root);

    Predicate namePredicate = criteriaBuilder.equal(root.get("name"), name);
    select.where(namePredicate);// www.  j  a v  a 2s.  c o m

    TypedQuery<OpenHubLicenseEntity> query = getEntityManager().createQuery(criteriaQuery);

    OpenHubLicenseEntity result = null;

    try {
        result = query.getSingleResult();
    } catch (NoResultException ex) {

    } catch (NonUniqueResultException ex) {

    }

    return result;
}

From source file:com.ttech.cordovabuild.domain.application.ApplicationRepositoryImpl.java

@Override
public List<ApplicationBuilt> getApplications(User owner) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ApplicationBuilt> cq = cb.createQuery(ApplicationBuilt.class);
    Root<ApplicationBuilt> from = cq.from(ApplicationBuilt.class);
    return em/*from ww w.ja v  a2  s.com*/
            .createQuery(cq.select(from)
                    .where(cb.equal(from.get(ApplicationBuilt_.application).get(Application_.owner), owner)))
            .getResultList();
}

From source file:ro.alexdutescu.coduribare.service.AbstractFacade.java

public int count() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
    cq.select(getEntityManager().getCriteriaBuilder().count(rt));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static int viewListCount(User user, String moduleResourceUri, String cloudServiceName)
        throws ConfigurationException, ValidationException {
    int count = 0;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {/*from   w w  w  .j  a va2  s. com*/
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Long> critQuery = builder.createQuery(Long.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(builder.count(rootQuery));
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        TypedQuery<Long> query = em.createQuery(critQuery);
        count = (int) (long) query.getSingleResult();
    } finally {
        em.close();
    }
    return count;
}