Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

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

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllSessionRecordings = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<SessionRecordingImpl>>() {
                @Override/*w  ww .  java2 s. c o  m*/
                public CriteriaQuery<SessionRecordingImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<SessionRecordingImpl> criteriaQuery = cb
                            .createQuery(SessionRecordingImpl.class);
                    final Root<SessionRecordingImpl> definitionRoot = criteriaQuery
                            .from(SessionRecordingImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });
}

From source file:org.verinice.persistence.VeriniceAccountDaoImpl.java

private TypedQuery<Entity> buildQueryForProperties(String loginName) {

    CriteriaBuilder builder = getCriteriaBuilder();

    CriteriaQuery<Entity> query = builder.createQuery(Entity.class);
    Root<Entity> entity = query.from(Entity.class);
    Join<PropertyList, Entity> propertyListJoin = entity.join("propertyLists");
    Join<PropertyList, Property> propertiesJoin = propertyListJoin.join("properties");
    List<Predicate> conditions = new ArrayList<>();
    Path<String> propertytypePath = propertiesJoin.get("propertytype");
    conditions.add(builder.like(propertytypePath, "configuration_benutzername"));
    Path<String> propertyvaluePath = propertiesJoin.get("propertyvalue");
    conditions.add(builder.like(propertyvaluePath, loginName));
    query.where(conditions.toArray(new Predicate[conditions.size()]));

    return entityManager.createQuery(query);
}

From source file:com.greendot.db.jpa.core.AbstractSearchDao.java

@Transactional(readOnly = true)
public long count() {

    final CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<Long> rootQuery = criteriaBuilder.createQuery(Long.class);
    rootQuery.select(criteriaBuilder.count(rootQuery.from(getEntityType())));

    return getEntityManager().createQuery(rootQuery).getSingleResult();
}

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

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllUserSessionUrl = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<UserSessionUrlImpl>>() {
                @Override// w ww  .  j av a2s  . c  o  m
                public CriteriaQuery<UserSessionUrlImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<UserSessionUrlImpl> criteriaQuery = cb
                            .createQuery(UserSessionUrlImpl.class);
                    final Root<UserSessionUrlImpl> definitionRoot = criteriaQuery
                            .from(UserSessionUrlImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });

}

From source file:nc.noumea.mairie.organigramme.core.dao.PersistentManager.java

@SuppressWarnings("unchecked")
private TypedQuery<T> constructTypedQueryByPropertyOrderBy(Class<? extends T> classe, String property,
        Object value, String orderByProperty) {
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<T> c = (CriteriaQuery<T>) qb.createQuery(classe);
    Root<T> p = (Root<T>) c.from(classe);
    Predicate condition = qb.equal(p.get(property), value);
    c.where(condition);/*from ww w  . j  a v  a2s .  c o m*/
    if (orderByProperty != null) {
        c.orderBy(qb.asc(p.get(orderByProperty)));
    }
    TypedQuery<T> q = em.createQuery(c);
    return q;
}

From source file:net.przemkovv.sphinx.dao.impl.DefaultGenericDAO.java

@Transactional(readOnly = true)
@Override//from  w w  w.j a v  a 2s .c o  m
public List<T> getAll() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(type);
    cq.from(type);
    return em.createQuery(cq).getResultList();
}

From source file:com.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java

/**
 * {@inheritDoc}// w w  w . j  av  a2  s. co  m
 */
public long getBulkUploadsCount(Map<String, Object> filters) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<BulkUpload> root = query.from(BulkUpload.class);
    query.select(builder.count(root));
    addFilterCriteria(filters, builder, root, query);

    return entityManager.createQuery(query).getSingleResult();
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> getAllByDescendingNameFiltered(ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> root = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, root);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from w ww .  j a  v a2s .  c o m*/

    criteria.orderBy(cb.desc(root.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}

From source file:de.tudarmstadt.ukp.csniper.webapp.security.dao.AbstractDao.java

/**
 * Finds all entities that have the same type as the given example and all fields are equal to
 * non-null fields in the example./*from  ww  w  .  ja  v  a2  s  .c  o  m*/
 */
protected <TT> CriteriaQuery<TT> queryByExample(TT aExample, String aOrderBy, boolean aAscending) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    @SuppressWarnings("unchecked")
    CriteriaQuery<TT> query = cb.createQuery((Class<TT>) aExample.getClass());
    @SuppressWarnings("unchecked")
    Root<TT> root = query.from((Class<TT>) aExample.getClass());
    query.select(root);

    List<Predicate> predicates = new ArrayList<Predicate>();
    BeanWrapper a = PropertyAccessorFactory.forBeanPropertyAccess(aExample);

    // Iterate over all properties
    for (PropertyDescriptor d : a.getPropertyDescriptors()) {
        Object value = a.getPropertyValue(d.getName());

        // Only consider writeable properties. This filters out e.g. the "class" (getClass())
        // property.
        if (value != null && a.isWritableProperty(d.getName())) {
            predicates.add(cb.equal(root.get(d.getName()), value));
        }
    }

    if (!predicates.isEmpty()) {
        query.where(predicates.toArray(new Predicate[predicates.size()]));
    }

    if (aOrderBy != null) {
        if (aAscending) {
            query.orderBy(cb.asc(root.get(aOrderBy)));
        } else {
            query.orderBy(cb.desc(root.get(aOrderBy)));
        }
    }

    return query;
}

From source file:eu.uqasar.service.ProductService.java

public List<Product> getAllByAscendingNameFiltered(
        eu.uqasar.web.pages.products.panels.ProductFilterStructure filter, int first, int count) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Product> criteria = cb.createQuery(Product.class);
    Root<Product> from = criteria.from(Product.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }//w  w  w . j a  va  2s .co  m

    criteria.orderBy(cb.asc(from.get(Product_.releaseDate)));
    return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList();

}