Search in sources :

Example 1 with JpaQuery

use of org.eclipse.persistence.jpa.JpaQuery in project cuba by cuba-platform.

the class FetchGroupManager method applyView.

private void applyView(JpaQuery query, String queryString, AttributeGroup attrGroup, View view, boolean singleResultExpected) {
    boolean useFetchGroup = attrGroup instanceof FetchGroup;
    FetchGroupDescription description = calculateFetchGroup(queryString, view, singleResultExpected, useFetchGroup);
    if (attrGroup instanceof FetchGroup)
        ((FetchGroup) attrGroup).setShouldLoadAll(true);
    if (log.isTraceEnabled())
        log.trace((useFetchGroup ? "Fetch" : "Load") + " group for " + view + ":\n" + description.getAttributes().stream().collect(Collectors.joining("\n")));
    for (String attribute : description.getAttributes()) {
        attrGroup.addAttribute(attribute);
    }
    MetaClass metaClass = metadata.getClassNN(view.getEntityClass());
    if (!metadataTools.isCacheable(metaClass)) {
        query.setHint(useFetchGroup ? QueryHints.FETCH_GROUP : QueryHints.LOAD_GROUP, attrGroup);
    }
    if (log.isDebugEnabled()) {
        String fetchModes = description.getHints().entrySet().stream().map(e -> e.getKey() + "=" + (e.getValue().equals(QueryHints.LEFT_FETCH) ? "JOIN" : "BATCH")).collect(Collectors.joining(", "));
        log.debug("Fetch modes for " + view + ": " + (fetchModes.equals("") ? "<none>" : fetchModes));
    }
    for (Map.Entry<String, String> entry : description.getHints().entrySet()) {
        query.setHint(entry.getValue(), entry.getKey());
    }
    if (description.hasBatches()) {
        query.setHint(QueryHints.BATCH_TYPE, "IN");
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) StringUtils(org.apache.commons.lang3.StringUtils) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Inject(javax.inject.Inject) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) ClassUtils(org.springframework.util.ClassUtils) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) QueryHints(org.eclipse.persistence.config.QueryHints) Collectors(java.util.stream.Collectors) Preconditions(com.haulmont.bali.util.Preconditions) Component(org.springframework.stereotype.Component) FetchGroup(org.eclipse.persistence.queries.FetchGroup) LoadGroup(org.eclipse.persistence.queries.LoadGroup) BaseUuidEntity(com.haulmont.cuba.core.entity.BaseUuidEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) FetchGroup(org.eclipse.persistence.queries.FetchGroup)

Example 2 with JpaQuery

use of org.eclipse.persistence.jpa.JpaQuery in project cuba by cuba-platform.

the class QueryImpl method getQuery.

protected JpaQuery<T> getQuery() {
    if (query == null) {
        View view = views.isEmpty() ? null : views.get(0);
        if (isNative) {
            log.trace("Creating SQL query: {}", queryString);
            if (resultClass == null)
                query = (JpaQuery) emDelegate.createNativeQuery(queryString);
            else {
                if (!Entity.class.isAssignableFrom(resultClass)) {
                    throw new IllegalArgumentException("Non-entity result class for native query is not supported" + " by EclipseLink: " + resultClass);
                }
                Class effectiveClass = metadata.getExtendedEntities().getEffectiveClass(resultClass);
                query = (JpaQuery) emDelegate.createNativeQuery(queryString, effectiveClass);
            }
        } else {
            log.trace("Creating JPQL query: {}", queryString);
            transformedQueryString = transformQueryString();
            log.trace("Transformed JPQL query: {}", transformedQueryString);
            Class effectiveClass = getEffectiveResultClass();
            query = buildJPAQuery(transformedQueryString, effectiveClass);
            if (view != null) {
                MetaClass metaClass = metadata.getClassNN(view.getEntityClass());
                if (!metadata.getTools().isCacheable(metaClass) || !singleResultExpected) {
                    query.setHint(QueryHints.REFRESH, HintValues.TRUE);
                    query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeByMapping);
                }
            }
        }
        if (flushMode == null) {
            if (view != null && !view.loadPartialEntities()) {
                query.setFlushMode(FlushModeType.AUTO);
            } else {
                query.setFlushMode(FlushModeType.COMMIT);
            }
        } else {
            query.setFlushMode(flushMode);
        }
        boolean nullParam = false;
        for (Param param : params) {
            param.apply(query);
            if (param.value == null)
                nullParam = true;
        }
        addMacroParams(query);
        // disable SQL caching to support "is null" generation
        if (nullParam)
            query.setHint(QueryHints.PREPARE, HintValues.FALSE);
        // nested collections in some cases
        if (maxResults != null && !singleResultExpected)
            query.setMaxResults(maxResults);
        if (firstResult != null && !singleResultExpected)
            query.setFirstResult(firstResult);
        if (lockMode != null)
            query.setLockMode(lockMode);
        if (hints != null) {
            for (Map.Entry<String, Object> hint : hints.entrySet()) {
                hintsProcessor.applyQueryHint(query, hint.getKey(), hint.getValue());
            }
        }
        for (int i = 0; i < views.size(); i++) {
            if (i == 0)
                fetchGroupMgr.setView(query, queryString, views.get(i), singleResultExpected);
            else
                fetchGroupMgr.addView(query, queryString, views.get(i), singleResultExpected);
        }
    }
    // noinspection unchecked
    return query;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 3 with JpaQuery

use of org.eclipse.persistence.jpa.JpaQuery in project querydsl by querydsl.

the class EclipseLinkHandler method iterate.

@SuppressWarnings("unchecked")
@Override
public <T> CloseableIterator<T> iterate(Query query, FactoryExpression<?> projection) {
    boolean canUseCursor = false;
    try {
        canUseCursor = query.unwrap(Query.class) instanceof JpaQuery;
    }// can't unwrap, just ignore the exception
     catch (PersistenceException e) {
    }
    Iterator<T> iterator = null;
    Closeable closeable = null;
    if (canUseCursor) {
        query.setHint(QueryHints.CURSOR, HintValues.TRUE);
        final Cursor cursor = (Cursor) query.getSingleResult();
        final int pageSize = cursor.getPageSize();
        closeable = new Closeable() {

            @Override
            public void close() throws IOException {
                cursor.close();
            }
        };
        iterator = new Iterator<T>() {

            private int rowsSinceLastClear = 0;

            @Override
            public boolean hasNext() {
                return cursor.hasNext();
            }

            @Override
            public T next() {
                if (rowsSinceLastClear++ == pageSize) {
                    rowsSinceLastClear = 0;
                    cursor.clear();
                }
                return (T) cursor.next();
            }
        };
    } else {
        iterator = query.getResultList().iterator();
    }
    if (projection != null) {
        return new TransformingIterator<T>(iterator, closeable, projection);
    } else {
        return new IteratorAdapter<T>(iterator, closeable);
    }
}
Also used : IteratorAdapter(com.mysema.commons.lang.IteratorAdapter) Closeable(java.io.Closeable) PersistenceException(javax.persistence.PersistenceException) IOException(java.io.IOException) Cursor(org.eclipse.persistence.queries.Cursor) JpaQuery(org.eclipse.persistence.jpa.JpaQuery)

Aggregations

JpaQuery (org.eclipse.persistence.jpa.JpaQuery)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 Entity (com.haulmont.cuba.core.entity.Entity)2 Preconditions (com.haulmont.bali.util.Preconditions)1 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Range (com.haulmont.chile.core.model.Range)1 BaseUuidEntity (com.haulmont.cuba.core.entity.BaseUuidEntity)1 EmbeddableEntity (com.haulmont.cuba.core.entity.EmbeddableEntity)1 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 IteratorAdapter (com.mysema.commons.lang.IteratorAdapter)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 Inject (javax.inject.Inject)1