Example usage for javax.persistence.criteria CriteriaQuery select

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

Introduction

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

Prototype

CriteriaQuery<T> select(Selection<? extends T> selection);

Source Link

Document

Specify the item that is to be returned in the query result.

Usage

From source file:org.businessmanager.dao.GenericDaoImpl.java

@Override
public Long getCount(Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteriaQuery = queryBuilder.createQuery(Long.class);
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());
    CriteriaQuery<Long> select = criteriaQuery.select(queryBuilder.count(rootQuery));

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);//from ww  w  . j  a v a  2s. c o  m
    criteriaQuery.where(predicateList.toArray(new Predicate[0]));

    TypedQuery<Long> typedQuery = entityManager.createQuery(select);
    return typedQuery.getSingleResult();
}

From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java

@Override
public Translation readTranslation(TranslatedEntity entityType, String entityId, String fieldName,
        String localeCode, String localeCountryCode, ResultType stage) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class);
    Root<TranslationImpl> root = criteria.from(TranslationImpl.class);
    criteria.select(root);
    List<Predicate> restrictions = new ArrayList<Predicate>();
    restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType()));
    restrictions.add(builder.equal(root.get("entityId"), entityId));
    restrictions.add(builder.equal(root.get("fieldName"), fieldName));
    restrictions.add(builder.like(root.get("localeCode").as(String.class), localeCode + "%"));
    try {/*from  w  w  w  .j a  v a2s  .  co m*/
        if (extensionManager != null) {
            extensionManager.getProxy().setup(TranslationImpl.class, stage);
            extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root,
                    restrictions);
        }
        criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));

        TypedQuery<Translation> query = em.createQuery(criteria);
        query.setHint(QueryHints.HINT_CACHEABLE, true);
        List<Translation> translations = query.getResultList();

        if (!translations.isEmpty()) {
            return findBestTranslation(localeCountryCode, translations);
        } else {
            return null;
        }
    } finally {
        if (extensionManager != null) {
            extensionManager.getProxy().breakdown(TranslationImpl.class, stage);
        }
    }
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * use aliases/*from  w w w.ja  v  a  2s  . co  m*/
 */
@Transactional
public void doSelect4() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> c = cb.createQuery(String.class);
    Root<Employee> e = c.from(Employee.class);
    c.select(e.<String>get("name").alias("fullname"));
    showResult(c);
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * SELECT e.name FROM jpa_query_employee'
 *///from w ww  . j a v a  2 s.  c o  m
@Transactional
public void doSelect1() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> c = cb.createQuery(String.class);
    Root<Employee> e = c.from(Employee.class);
    c.select(e.<String>get("name"));

    showResult(c);
}

From source file:org.jnap.core.persistence.jpa.DaoSupport.java

/**
 * //from  ww  w .j  a va  2s  .  c  o  m
 * @return
 */
protected CriteriaQuery<E> createCriteriaQuery() {
    final CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<E> criteria = builder.createQuery(getEntityClass());
    criteria.select(criteria.from(getEntityClass()));
    return criteria;
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

public <K> T findBy(String fieldName, Object value, Class<K> valueClass) {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);
    cq.select(root).where(cb.equal(root.get(fieldName).as(valueClass), value));
    List<T> list = em.createQuery(cq).getResultList();
    return list.isEmpty() ? null : list.get(0);
}

From source file:org.emau.icmvc.ganimed.epix.core.internal.PersonPreprocessedCache.java

/**
 * Load all personpreprocessed entities from database
 *
 * @param em//  w  w  w.j a  va2 s. c o m
 * @return
 */
private List<PersonPreprocessed> getAllPersonPreprocessed(EntityManager em) {
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<PersonPreprocessed> criteriaQuery = criteriaBuilder.createQuery(PersonPreprocessed.class);
    Root<PersonPreprocessed> root = criteriaQuery.from(PersonPreprocessed.class);
    criteriaQuery.select(root);
    List<PersonPreprocessed> personPreprocessedList = em.createQuery(criteriaQuery).getResultList();
    return personPreprocessedList;
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * SELECT e FROM jpa_query_employee/*from www . ja v a2s. co m*/
 */
@Transactional
public void doSimple() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);
    c.select(e);

    showResult(c);
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * SELECT e FROM jpa_query_employee e WHERE e.name='employee_1'
 *//*from w w  w .ja  v a 2s .c  om*/
@Transactional
public void doSimple2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);
    c.select(e).where(cb.equal(e.get("name"), "employee_1"));

    showResult(c);
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaMarketplaceRatingDao.java

/**
 * @since 5.0/*from   www  .java  2s . c  om*/
 * @param marketplaceRatingPK the primary key of the entity you want
 * @return Set of ratings per portlet definition
 */
@PortalTransactionalReadOnly
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
public Set<IMarketplaceRating> getRatingsByFname(String fname) {

    //Build criteria to fetch MarketplaceRatingImpl based on the incoming portlet name. 
    final EntityManager entityManager = this.getEntityManager();
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    final CriteriaQuery<IMarketplaceRating> getByPortlet = cb.createQuery(IMarketplaceRating.class);
    final Root<MarketplaceRatingImpl> imr = getByPortlet.from(MarketplaceRatingImpl.class);
    getByPortlet.select(imr);

    //Define the path to the portlet fName
    final Path<MarketplaceRatingPK> mrPK = imr.get("marketplaceRatingPK");
    final Path<PortletDefinitionImpl> mrIPD = mrPK.get("portletDefinition");

    final ParameterExpression<String> portletFName = cb.parameter(String.class, "portletFName");

    getByPortlet.where(cb.equal(mrIPD.get("fname"), portletFName));
    TypedQuery<IMarketplaceRating> tq = entityManager.createQuery(getByPortlet);
    tq.setParameter("portletFName", fname);
    List<IMarketplaceRating> resultList = tq.getResultList();
    Set<IMarketplaceRating> resultSet = new HashSet<IMarketplaceRating>(resultList);
    return resultSet;

}