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

public List<T> findByAssignedEntity(ListAttribute<T, ?> listAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(listAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);/*from   ww  w  .j a v a  2 s.c om*/

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}

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

public List<T> findByAssignedEntity(SetAttribute<T, ?> setAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(setAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);/*  w w w  .j  a v a  2  s  .  c o m*/

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

public List<T> findList(Integer first, Integer count, List<Filter> filters, List<Order> orders) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass);
    criteriaQuery.select(criteriaQuery.from(entityClass));
    return findList(criteriaQuery, first, count, filters, orders);
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

public Page<T> findPage(Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass);
    criteriaQuery.select(criteriaQuery.from(entityClass));
    return findPage(criteriaQuery, pageable);
}

From source file:com.zero.dao.impl.BaseDaoImpl.java

public long count(Filter... filters) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityClass);
    criteriaQuery.select(criteriaQuery.from(entityClass));
    return count(criteriaQuery, filters != null ? Arrays.asList(filters) : null);
}

From source file:net.echinopsii.ariane.community.plugin.rabbitmq.directory.RabbitmqDirectoryBootstrap.java

private void plugDirectoryJPAProvider() {
    Company pivotal = null;/*  ww w .j av  a2 s .c o  m*/
    Application rabbitmq = null;

    directoryJpaProvider.addSubPersistenceBundle(FrameworkUtil.getBundle(RabbitmqDirectoryBootstrap.class));

    EntityManager em = directoryJpaProvider.createEM();
    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<Company> cmpCriteria = builder.createQuery(Company.class);
    Root<Company> cmpRoot = cmpCriteria.from(Company.class);
    cmpCriteria.select(cmpRoot).where(builder.equal(cmpRoot.<String>get("name"), "Pivotal"));
    TypedQuery<Company> cmpQuery = em.createQuery(cmpCriteria);
    try {
        pivotal = cmpQuery.getSingleResult();
        log.debug("Pivotal company already defined ...");
    } catch (NoResultException e) {
        log.debug("Pivotal company will be defined ...");
    } catch (Exception e) {
        throw e;
    }

    CriteriaQuery<Application> appCriteria = builder.createQuery(Application.class);
    Root<Application> appRoot = appCriteria.from(Application.class);
    appCriteria.select(appRoot).where(builder.equal(appRoot.<String>get("name"), "RabbitMQ"));
    TypedQuery<Application> appQuery = em.createQuery(appCriteria);
    try {
        rabbitmq = appQuery.getSingleResult();
        log.debug("RabbitMQ application already defined ...");
    } catch (NoResultException e) {
        log.debug("RabbitMQ application will be defined ...");
    } catch (Exception e) {
        throw e;
    }

    em.getTransaction().begin();

    if (pivotal == null) {
        pivotal = new Company().setNameR("Pivotal").setDescriptionR("Pivotal");
        em.persist(pivotal);
    }

    if (rabbitmq == null) {
        rabbitmq = new Application().setNameR("RabbitMQ").setCompanyR(pivotal).setShortNameR("RabbitMQ")
                .setColorCodeR("ff6600").setDescriptionR("Robust messaging for applications");
        em.persist(rabbitmq);
    }

    if (!pivotal.getApplications().contains(rabbitmq)) {
        pivotal.getApplications().add(rabbitmq);
    }

    em.flush();
    em.getTransaction().commit();
}

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

@Override
public List<T> findByAttributes(Map<SingularAttribute<T, ?>, Object> theAttributes) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());
    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> aPredicateList = new ArrayList<Predicate>();
    Iterator<SingularAttribute<T, ?>> anIterator = theAttributes.keySet().iterator();
    while (anIterator.hasNext()) {
        SingularAttribute<T, ?> aKey = anIterator.next();
        Object aValue = theAttributes.get(aKey);
        Predicate aPredicate = queryBuilder.equal(rootQuery.get(aKey), aValue);
        aPredicateList.add(aPredicate);// w w w .j a va  2  s  . c om
    }

    select.where(aPredicateList.toArray(new Predicate[0]));
    TypedQuery<T> typedQuery = entityManager.createQuery(select);

    return typedQuery.getResultList();
}

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

@Override
public void afterPropertiesSet() throws Exception {
    this.nowParameter = this.createParameterExpression(DateTime.class, "now");

    this.deletePortalCookieQueryString = "DELETE FROM " + PortalCookieImpl.class.getName() + " e " + "WHERE e."
            + PortalCookieImpl_.expires.getName() + " <= :" + this.nowParameter.getName();

    this.deleteEmptyPortalCookieQueryString = "DELETE FROM " + PortalCookieImpl.class.getName() + " e "
            + "WHERE e." + PortalCookieImpl_.expires.getName() + " <= :" + this.nowParameter.getName() + " AND "
            + "e." + PortalCookieImpl_.portletCookies.getName() + " IS EMPTY";

    this.deletePortletCookieQueryString = "DELETE FROM " + PortletCookieImpl.class.getName() + " e "
            + "WHERE e." + PortletCookieImpl_.expires.getName() + " <= :" + this.nowParameter.getName();

    this.findExpiredByParentPortletCookiesQuery = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletCookieImpl>>() {
                @Override/*from  www  . j  a  v  a  2 s . c  om*/
                public CriteriaQuery<PortletCookieImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<PortletCookieImpl> criteriaQuery = cb
                            .createQuery(PortletCookieImpl.class);
                    final Root<PortletCookieImpl> typeRoot = criteriaQuery.from(PortletCookieImpl.class);
                    criteriaQuery.select(typeRoot);
                    criteriaQuery.where(cb.lessThanOrEqualTo(
                            typeRoot.get(PortletCookieImpl_.portalCookie).get(PortalCookieImpl_.expires),
                            nowParameter));

                    return criteriaQuery;
                }
            });
}

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

/**
 * multiple select//ww  w  .  j a va  2 s . c  o m
 */
public void doSelect2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();

    // method1: tuple
    CriteriaQuery<Tuple> tupleQuery = cb.createTupleQuery();
    Root<Employee> e1 = tupleQuery.from(Employee.class);
    tupleQuery.select(cb.tuple(e1.get("id"), e1.get("name")));
    showResult(tupleQuery);

    // method2: object[] and criteriaquery.multiselec
    CriteriaQuery<Object[]> multiQuery = cb.createQuery(Object[].class);
    Root<Employee> e2 = multiQuery.from(Employee.class);
    multiQuery.multiselect(e2.get("id"), e2.get("name"));
    showResult(multiQuery);
}

From source file:com.sfs.captor.controller.RequirementAction.java

/**
 * Get count of requirement rules/*w  w w .  jav  a  2  s .co  m*/
 * 
 * @return count
 */
private Long getRequirementRuleCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> c = cb.createQuery(Long.class);
    c.select(cb.count(c.from(RequirementRule.class)));
    return em.createQuery(c).getSingleResult();
}