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:net.dontdrinkandroot.persistence.dao.GenericJpaDao.java

@Override
@Transactional(propagation = Propagation.MANDATORY, readOnly = true)
public <E extends Entity<K>, K> long getCount(final Class<E> clazz) {
    final CriteriaBuilder builder = this.getCriteriaBuilder();
    final CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
    final Root<E> from = criteriaQuery.from(clazz);

    final Expression<Long> count = builder.count(from);

    criteriaQuery.select(count);//from  w w w .j  av  a2 s .c  o  m

    return this.findSingle(criteriaQuery).longValue();
}

From source file:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java

/**
 * Get the {@link AuthenticationType}s out of the database.
 *
 * @param enabled/* www.ja  v a 2s.c  o m*/
 *            if not <code>null</code> and <code>true</code> only the
 *            enabled {@link AuthenticationType}s are replied.
 * @return the list of {@link AuthenticationType}s.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public List<AuthenticationType> getAuthenticationTypes(final Boolean enabled) {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    Predicate ands = cb.conjunction();
    ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), this.getContextName()));
    if (enabled != null) {
        ands.getExpressions().add(cb.equal(c.<Boolean>get(AuthenticationType_.enabled), enabled));
    }
    q.where(ands);
    q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName)));
    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    List<AuthenticationType> list = typedQuery.getResultList();
    for (AuthenticationType auth : list) {
        for (GrantedAuthorityType gr : auth.getGrantedAuthority()) {
            gr.getRoleName();
        }
        for (int i = 0; i < auth.getGrantedAuthority().size(); i++) {
            // eager loading...
            auth.getGrantedAuthority().get(i);
        }
    }
    return list;
}

From source file:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java

/**
 * Get the granted number of calls./* www  .  j  av a2 s  . c om*/
 *
 * @param userName
 *            the user name.
 * @param serviceName
 *            the service name.
 * @param operationName
 *            the operation name.
 * @return the number of calls.
 */
@Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public int getGrantedRequestNumber(final String userName, final String serviceName,
        final String operationName) {
    Integer requests = 0;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class);
    Root<AuthenticationType> c = q.from(AuthenticationType.class);
    q.where(cb.equal(c.<String>get(AuthenticationType_.context), this.contextNameProvider.getContextName()),
            cb.equal(c.<String>get(AuthenticationType_.userName), userName));

    TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q);
    try {
        AuthenticationType log = typedQuery.getSingleResult();
        for (GrantedAuthorityType role : log.getGrantedAuthority()) {
            if (this.getRoleName(serviceName, operationName).equals(role.getRoleName())) {
                requests = role.getMaxRequests();
            }
        }
        if (requests == null) {
            requests = Integer.MAX_VALUE;
        }
    } catch (Exception e) {
        requests = 0;
    }
    return requests;
}

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

/**
 * {@inheritDoc}/*w  w  w  . ja  v a  2s  .  c o m*/
 */
public List<Transaction> findTransactionsByStatementDateAndCreditCardId(Date startDate, Date endDate,
        long creditCardId) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> root = query.from(Transaction.class);
    query.select(root);
    Predicate date = builder.greaterThan(root.<Date>get(FIELD_CONSTANT_TRANSACTION_DATE), startDate);
    Predicate isCreditCardId = builder.equal(root.<String>get(FIELD_CONSTANT_CREDIT_CARD_ID), creditCardId);
    query.where(builder.and(date, isCreditCardId));
    return super.find(query);
}

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

/**
 * {@inheritDoc}//  ww w .j  a v  a 2s. c  om
 */
public List<Transaction> getTransactions(int start, int limit, String sortField, String sortOrder,
        Map<String, Object> filters, long userId) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> root = query.from(Transaction.class);
    query.select(root);
    addSorting(sortField, sortOrder, query, builder, root);
    addFilterCriteria(userId, filters, builder, root, query);

    return entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit).getResultList();

}

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

/**
 * {@inheritDoc}/*from w  ww. jav a 2 s  . c  om*/
 */
public List<Transaction> searchTransaction(String searchTerm) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> root = query.from(Transaction.class);
    query.select(root);
    Predicate customerName = builder.like(root.<String>get(FIELDCONSTANT_CUSTOMERNAME),
            MODULO + searchTerm + MODULO);
    Predicate creditCardNumber = builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER),
            MODULO + searchTerm + MODULO);
    Predicate description = builder.like(root.<String>get(FIELD_CONSTANT_DESCRIPTION),
            MODULO + searchTerm + MODULO);
    query.where(builder.or(customerName, creditCardNumber, description));
    return super.find(query);
}

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

/**
 * {@inheritDoc}//w  w  w .  j  a  v a 2  s  .  c  o  m
 */
public Transaction findLastTransaction() {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> root = query.from(Transaction.class);
    query.select(root);
    query.orderBy(builder.desc(root.get(FIELD_CONSTANT_TRANSACTION_ID)));
    try {
        return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
    } catch (NoResultException e) {
        LOGGER.error("Error while retrieving last transaction", e);
    }
    return null;

}

From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java

/**
 * Find an entity of the given type, using as filter criteria the specified
 * list of attribute values. The filter criteria must uniquely identify an
 * entity./* w ww . j av a  2  s  .  c om*/
 * 
 * @param entityClass
 * @param params
 * @return
 * @throws BusinessException
 */
public <T> T findEntityByAttributes(Class<T> entityClass, Map<String, Object> params) {
    CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);
    cq.select(root);
    Assert.notNull(params);
    Predicate p = null;
    if (entityClass.isAssignableFrom(IModelWithClientId.class)) {
        p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId());
    }
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue()));
    }
    cq.where(p);
    TypedQuery<T> query = this.getEntityManager().createQuery(cq);
    return (T) query.getSingleResult();
}

From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java

/**
 * Find a list of entities of the given type, using as filter criteria the
 * specified list of attribute values./* w  w  w  .  j a va 2 s .c o  m*/
 * 
 * @param entityClass
 * @param params
 * @return
 * @throws BusinessException
 */
public <T> List<T> findEntitiesByAttributes(Class<T> entityClass, Map<String, Object> params) {
    CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);
    cq.select(root);
    Assert.notNull(params);
    Predicate p = null;
    if (entityClass.isAssignableFrom(IModelWithClientId.class)) {
        p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId());
    }
    for (Map.Entry<String, Object> entry : params.entrySet()) {
        p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue()));
    }
    cq.where(p);
    TypedQuery<T> query = this.getEntityManager().createQuery(cq);
    return (List<T>) query.getResultList();
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public List<T> find() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass);
    Root<T> customer = criteriaQuery.from(entityClass);
    criteriaQuery.getRoots().add(customer);
    Query query = em.createQuery(criteriaQuery);
    return query.getResultList();
}