Search in sources :

Example 1 with Parameters

use of org.apache.deltaspike.data.impl.param.Parameters in project deltaspike by apache.

the class MethodQueryBuilder method createJpaQuery.

private Query createJpaQuery(CdiQueryInvocationContext context) {
    Parameters params = context.getParams();
    QueryRoot root = context.getRepositoryMethodMetadata().getQueryRoot();
    String jpqlQuery = context.applyQueryStringPostProcessors(root.getJpqlQuery());
    context.setQueryString(jpqlQuery);
    params.updateValues(root.getParameterUpdates());
    Query result = params.applyTo(context.getEntityManager().createQuery(jpqlQuery));
    return context.applyRestrictions(result);
}
Also used : Parameters(org.apache.deltaspike.data.impl.param.Parameters) QueryRoot(org.apache.deltaspike.data.impl.builder.part.QueryRoot) Query(javax.persistence.Query)

Example 2 with Parameters

use of org.apache.deltaspike.data.impl.param.Parameters in project deltaspike by apache.

the class AnnotatedQueryBuilder method createJpaQuery.

private javax.persistence.Query createJpaQuery(Query query, CdiQueryInvocationContext context) {
    EntityManager entityManager = context.getEntityManager();
    Parameters params = context.getParams();
    javax.persistence.Query result = null;
    if (isNotEmpty(query.named())) {
        if (!context.hasQueryStringPostProcessors()) {
            result = params.applyTo(entityManager.createNamedQuery(query.named()));
        } else {
            javax.persistence.Query namedQuery = entityManager.createNamedQuery(query.named());
            String named = factory.extract(namedQuery);
            String jpqlQuery = context.applyQueryStringPostProcessors(named);
            result = params.applyTo(entityManager.createQuery(jpqlQuery));
        }
    } else if (query.isNative()) {
        String jpqlQuery = context.applyQueryStringPostProcessors(query.value());
        Class<?> resultType = getQueryResultType(context.getMethod());
        if (isEntityType(resultType)) {
            result = params.applyTo(entityManager.createNativeQuery(jpqlQuery, resultType));
        } else {
            result = params.applyTo(entityManager.createNativeQuery(jpqlQuery));
        }
    } else {
        String jpqlQuery = context.applyQueryStringPostProcessors(query.value());
        context.setQueryString(jpqlQuery);
        result = params.applyTo(entityManager.createQuery(jpqlQuery));
    }
    return context.applyRestrictions(result);
}
Also used : EntityManager(javax.persistence.EntityManager) Parameters(org.apache.deltaspike.data.impl.param.Parameters)

Example 3 with Parameters

use of org.apache.deltaspike.data.impl.param.Parameters in project deltaspike by apache.

the class CdiQueryInvocationContext method applyRestrictions.

public Query applyRestrictions(Query query) {
    Parameters params = getParams();
    Method method = getMethod();
    if (params.hasSizeRestriction()) {
        query.setMaxResults(params.getSizeRestriciton());
    }
    if (params.hasFirstResult()) {
        query.setFirstResult(params.getFirstResult());
    }
    LockModeType lockMode = extractLockMode();
    if (lockMode != null) {
        query.setLockMode(lockMode);
    }
    QueryHint[] hints = extractQueryHints();
    if (hints != null) {
        for (QueryHint hint : hints) {
            query.setHint(hint.name(), hint.value());
        }
    }
    applyEntityGraph(query, method);
    query = applyJpaQueryPostProcessors(query);
    return query;
}
Also used : Parameters(org.apache.deltaspike.data.impl.param.Parameters) QueryHint(javax.persistence.QueryHint) LockModeType(javax.persistence.LockModeType) Method(java.lang.reflect.Method)

Example 4 with Parameters

use of org.apache.deltaspike.data.impl.param.Parameters in project deltaspike by apache.

the class CountQueryPostProcessor method postProcess.

@Override
public Query postProcess(CdiQueryInvocationContext context, Query query) {
    String queryString = getQueryString(context, query);
    QueryExtraction extract = new QueryExtraction(queryString);
    String count = extract.rewriteToCount();
    log.log(Level.FINER, "Rewrote query {0} to {1}", new Object[] { queryString, count });
    Query result = context.getEntityManager().createQuery(count);
    Parameters params = context.getParams();
    params.applyTo(result);
    return result;
}
Also used : Parameters(org.apache.deltaspike.data.impl.param.Parameters) Query(javax.persistence.Query)

Aggregations

Parameters (org.apache.deltaspike.data.impl.param.Parameters)4 Query (javax.persistence.Query)2 Method (java.lang.reflect.Method)1 EntityManager (javax.persistence.EntityManager)1 LockModeType (javax.persistence.LockModeType)1 QueryHint (javax.persistence.QueryHint)1 QueryRoot (org.apache.deltaspike.data.impl.builder.part.QueryRoot)1