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:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;/*w w  w. j  av a 2s  .  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:com.github.jinahya.persistence.ShadowTest.java

protected static List<Morton> MORTONS(final EntityManager manager, final int firstResult,
        final int maxResults) {

    final CriteriaBuilder builder = manager.getCriteriaBuilder();
    final CriteriaQuery<Morton> query = builder.createQuery(Morton.class);
    final Root<Morton> morton = query.from(Morton.class);

    query.select(morton).orderBy(builder.desc(morton.get(Morton_.id)));

    return manager.createQuery(query).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
}

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

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * @param em entity manager/*from w w  w .  j ava  2  s .c om*/
 * @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

/**
 * Create a query for fetching a single object
 * /*  w ww  .jav  a2s.com*/
 * @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 that fetches objects based on their IDs
 * // w w  w  .  ja  v  a2s. c  om
 * @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());
}

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

/**
 * Creates a query that simply selects some objects based on some filter
 * /*from ww w .  j  a  va2  s.com*/
 * @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 performs a count
 * //from  w w  w  .j a v a2s . co 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

/**
 * Creates a query used to retrieve a single entity based on a unique property value
 * /*from www .  j  a v  a2 s .c  o m*/
 * @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 to fetch an object based on a value of a unique property
 * /*from  ww w  .  j  a  va 2s.  co m*/
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param fetchJoins
 *            the fetch joins to include
 * @param propertyName
 *            name of the property to search on
 * @param value
 *            value of the property to search on
 * @return
 */
public static <T> CriteriaQuery<T> createUniquePropertyFetchQuery(EntityManager entityManager,
        Class<T> entityClass, FetchJoinInformation[] fetchJoins, String propertyName, Object value,
        boolean caseSensitive) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> cq = builder.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    addFetchJoinInformation(root, fetchJoins);

    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);
    cq.distinct(true);

    return cq;
}

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

@Override
public List<Solution> getSolutionsByTask(Task task) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Solution> cq = cb.createQuery(Solution.class);
    Root<Solution> solution = cq.from(Solution.class);
    cq.where(cb.equal(solution.get(Solution_.task), task));
    return em.createQuery(cq).getResultList();
}