Example usage for javax.persistence EntityManager getCriteriaBuilder

List of usage examples for javax.persistence EntityManager getCriteriaBuilder

Introduction

In this page you can find the example usage for javax.persistence EntityManager getCriteriaBuilder.

Prototype

public CriteriaBuilder getCriteriaBuilder();

Source Link

Document

Return an instance of CriteriaBuilder for the creation of CriteriaQuery objects.

Usage

From source file:org.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * @param session/*from ww  w  .  j ava2s .c om*/
 *            The session in which to execute the Persistence (JPA /
 *            EclipseLink) query.
 * @param summaryId
 *            The database ID of the summary for which to compute the
 *            corresponding Test Result nodes.
 * @return the Test Result nodes associated with the given Test Result
 *         Summary, sorted by sequence (ascending).
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean hasTestResultDetails(EntityManager session, Long summaryId) {
    boolean hasDetails = false;
    if (session == null) {
        return hasDetails;
    }

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestResultClass());
    query.select(builder.count(from)).where(builder.equal(from.get("internalTestResultSummaryID"), summaryId)); //$NON-NLS-1$

    Number result = (Number) session.createQuery(query).getSingleResult();
    if (result.longValue() > 0) {
        hasDetails = true;
    }
    return hasDetails;
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;//from  ww  w . j a  v  a  2 s.c o  m
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * @param em entity manager/* w ww .j av a  2 s .  co m*/
 * @param criteria source criteria
 * @return row count CriteriaQuery
 */
public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
    copyCriteriaWithoutSelectionAndOrder(criteria, countCriteria);

    Expression<Long> countExpression;

    if (criteria.isDistinct()) {
        countExpression = builder.countDistinct(findRoot(countCriteria, criteria.getResultType()));
    } else {
        countExpression = builder.count(findRoot(countCriteria, criteria.getResultType()));
    }

    return countCriteria.select(countExpression);
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a query that performs a count
 * //  w  w w .ja  v  a2 s . c o m
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param filter
 *            the filter to apply
 * @param distinct
 *            whether to return only distinct results
 * @return
 */
public static <T> CriteriaQuery<Long> createCountQuery(EntityManager entityManager, Class<T> entityClass,
        Filter filter, boolean distinct) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    Root<T> root = cq.from(entityClass);

    cq.select(distinct ? builder.countDistinct(root) : builder.count(root));

    Predicate p = createPredicate(filter, builder, root);
    if (p != null) {
        cq.where(p);
    }
    return cq;
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Create a query for fetching a single object
 * /*  w  ww  .ja  va2  s.  c  o  m*/
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param id
 *            ID of the object to return
 * @param fetchJoins
 *            fetch joins to include
 * @return
 */
public static <ID, T> CriteriaQuery<T> createFetchSingleObjectQuery(EntityManager entityManager,
        Class<T> entityClass, ID id, FetchJoinInformation[] fetchJoins) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    addFetchJoinInformation(root, fetchJoins);

    cq.where(builder.equal(root.get(DynamoConstants.ID), id));
    return cq;
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a query for retrieving the IDs of the entities that match the provided filter
 * // w w  w.  ja v  a2 s . c  o m
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param filter
 *            the filter to apply
 * @param sortOrder
 *            the sorting to apply
 * @return
 */
public static <T> CriteriaQuery<Tuple> createIdQuery(EntityManager entityManager, Class<T> entityClass,
        Filter filter, SortOrder... sortOrders) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = builder.createTupleQuery();
    Root<T> root = cq.from(entityClass);

    // select only the ID
    cq.multiselect(root.get(DynamoConstants.ID));

    // Set where clause
    Predicate p = createPredicate(filter, builder, root);
    if (p != null) {
        cq.where(p);
    }

    // add order clause - this is also important in case of an ID query
    // since we do need to return the correct IDs!
    return addSortInformation(builder, cq, root, sortOrders);
}

From source file:nl.b3p.viewer.stripes.ApplicationActionBean.java

static Application findApplication(String name, String version) {
    EntityManager em = Stripersist.getEntityManager();
    if (name != null) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery q = cb.createQuery(Application.class);
        Root<Application> root = q.from(Application.class);
        Predicate namePredicate = cb.equal(root.get("name"), name);
        Predicate versionPredicate = version != null ? cb.equal(root.get("version"), version)
                : cb.isNull(root.get("version"));
        q.where(cb.and(namePredicate, versionPredicate));
        try {/*from w  ww . ja v a2  s  .c  o m*/
            return (Application) em.createQuery(q).getSingleResult();
        } catch (NoResultException nre) {
            String decodedName = StringUtil.urlDecode(name);
            if (!decodedName.equals(name)) {
                return findApplication(decodedName, version);
            }
        }
    }
    return null;
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a query used to retrieve a single entity based on a unique property value
 * /*w ww  .j  a  va2s.  c om*/
 * @param entityManager
 * @param entityClass
 * @param propertyName
 * @param value
 * @return
 */
public static <T> CriteriaQuery<T> createUniquePropertyQuery(EntityManager entityManager, Class<T> entityClass,
        String propertyName, Object value, boolean caseSensitive) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    Predicate equals = null;
    if (value instanceof String && !caseSensitive) {
        equals = builder.equal(builder.upper(root.get(propertyName).as(String.class)),
                ((String) value).toUpperCase());
    } else {
        equals = builder.equal(root.get(propertyName), value);
    }
    cq.where(equals);

    return cq;
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a query that simply selects some objects based on some filter
 * //w w w. j av  a2  s  . co  m
 * @param filter
 *            the filter
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param sortOrder
 *            the sorting information
 * @return
 */
public static <T> CriteriaQuery<T> createSelectQuery(Filter filter, EntityManager entityManager,
        Class<T> entityClass, FetchJoinInformation[] fetchJoins, SortOrder... sortOrders) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    addFetchJoinInformation(root, fetchJoins);
    cq.select(root);

    Predicate p = createPredicate(filter, builder, root);
    if (p != null) {
        cq.where(p);
    }

    return addSortInformation(builder, cq, root, sortOrders);
}

From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a query that fetches objects based on their IDs
 * /*from   www  . ja  va2s  . c  o  m*/
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param ids
 *            the IDs of the desired entities
 * @param sortOrders
 *            the sorting information
 * @param fetchJoins
 *            the desired fetch joins
 * @return
 */
public static <ID, T> CriteriaQuery<T> createFetchQuery(EntityManager entityManager, Class<T> entityClass,
        List<ID> ids, SortOrders sortOrders, FetchJoinInformation[] fetchJoins) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    boolean distinct = addFetchJoinInformation(root, fetchJoins);

    Expression<String> exp = root.get(DynamoConstants.ID);
    cq.where(exp.in(ids));
    cq.distinct(distinct);

    return addSortInformation(builder, cq, root, sortOrders == null ? null : sortOrders.toArray());
}