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.eclipse.jubula.client.core.persistence.TestDataCubePM.java

/**
 * @param tdc/*from ww w . jav a2 s.c  o  m*/
 *            the test data cube to search for reusage
 * @param session
 *            The session into which the test cases will be loaded.
 * @return list of ITestDataCubePO
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<ITestDataCubePO> computeReuser(IParameterInterfacePO tdc, EntityManager session) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestDataCubeClass());
    query.select(from).where(builder.isNotNull(from.get("hbmReferencedDataCube"))); //$NON-NLS-1$

    List<ITestDataCubePO> queryResult = session.createQuery(query).getResultList();
    List<ITestDataCubePO> result = new ArrayList<ITestDataCubePO>();
    for (ITestDataCubePO pio : queryResult) {
        if (areEqual(pio.getReferencedDataCube(), tdc)) {
            result.add(pio);
        }
    }
    return result;
}

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

/**
 * @param pioToSearch//from  w w w . ja va2  s. c  o m
 *            the test data cube to search for reusage
 * @param session
 *            The session into which the test cases will be loaded.
 * @param proj
 *            the project to search in
 * @return list of param node po; without any ITestDataCubePOs themselves as
 *         they don't have a parent project id
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<IParamNodePO> computeParamNodeReuser(IParameterInterfacePO pioToSearch,
        EntityManager session, IProjectPO proj) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Root from = query.from(PoMaker.getTestCasePOClass());
    query.select(from).where(builder.equal(from.get("hbmParentProjectId"), proj.getId())); //$NON-NLS-1$

    List<IParameterInterfacePO> queryResult = session.createQuery(query).getResultList();
    List<IParamNodePO> result = new ArrayList<IParamNodePO>();
    for (IParameterInterfacePO pio : queryResult) {
        if (pio instanceof IParamNodePO) {
            IParamNodePO pn = (IParamNodePO) pio;
            if (areEqual(pn.getReferencedDataCube(), pioToSearch)) {
                result.add(pn);
            }
        }
    }
    return result;
}

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.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * @param session/*from   w w  w .j a va  2s. co  m*/
 *            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  www. jav  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:org.agric.oxm.utils.JpaUtils.java

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * /*from   ww w.ja v a 2  s  .  c  o m*/
 * @param em
 *            entity manager
 * @param criteria
 *            source criteria
 * @return row coutnt CriteriaQuery
 */
public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
    copyCriteriaNoSelection(criteria, countCriteria);
    countCriteria.select(builder.count(findRoot(countCriteria, criteria.getResultType())));

    return countCriteria;
}

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

/**
 * @param session//from  w ww .  ja  v  a2  s.c o  m
 *            The session in which to execute the Persistence (JPA / EclipseLink) query.
 * @return a list of test result ids that have test result details
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List<Number> computeTestresultIdsWithDetails(EntityManager session) {

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery();
    Path from = query.from(PoMaker.getTestResultClass()).get("internalTestResultSummaryID"); //$NON-NLS-1$
    query.select(from).distinct(true);

    return session.createQuery(query).getResultList();
}

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

public static <T> void copyCriteria(CriteriaQuery<T> from, CriteriaQuery<T> to) {
    copyCriteriaNoSelection(from, to);/* ww  w .j a v a  2 s . c om*/
    to.select(from.getSelection());
}

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

/**
 * Creates a query that performs a count
 * //from  w  w w  . j  a  v  a  2 s .  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 that simply selects some objects based on some filter
 * //from  w w w . jav  a2  s. c o 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);
}