Search in sources :

Example 1 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method list.

@Override
public List list(SharedSessionContractImplementor session, QueryParameters queryParameters) throws HibernateException {
    // Delegate to the QueryLoader...
    errorIfDML();
    final QueryNode query = (QueryNode) sqlAst;
    final boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits();
    final boolean needsDistincting = (query.getSelectClause().isDistinct() || hasLimit) && containsCollectionFetches();
    QueryParameters queryParametersToUse;
    if (hasLimit && containsCollectionFetches()) {
        LOG.firstOrMaxResultsSpecifiedWithCollectionFetch();
        RowSelection selection = new RowSelection();
        selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
        selection.setTimeout(queryParameters.getRowSelection().getTimeout());
        queryParametersToUse = queryParameters.createCopyUsing(selection);
    } else {
        queryParametersToUse = queryParameters;
    }
    List results = queryLoader.list(session, queryParametersToUse);
    if (needsDistincting) {
        int includedCount = -1;
        // NOTE : firstRow is zero-based
        int first = !hasLimit || queryParameters.getRowSelection().getFirstRow() == null ? 0 : queryParameters.getRowSelection().getFirstRow();
        int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows();
        List tmp = new ArrayList();
        IdentitySet distinction = new IdentitySet();
        for (final Object result : results) {
            if (!distinction.add(result)) {
                continue;
            }
            includedCount++;
            if (includedCount < first) {
                continue;
            }
            tmp.add(result);
            // NOTE : ( max - 1 ) because first is zero-based while max is not...
            if (max >= 0 && (includedCount - first) >= (max - 1)) {
                break;
            }
        }
        results = tmp;
    }
    return results;
}
Also used : QueryNode(org.hibernate.hql.internal.ast.tree.QueryNode) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) QueryParameters(org.hibernate.engine.spi.QueryParameters) RowSelection(org.hibernate.engine.spi.RowSelection) EntityGraphQueryHint(org.hibernate.engine.query.spi.EntityGraphQueryHint)

Example 2 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class Loader method loadEntityBatch.

/**
	 * Called by wrappers that batch load entities
	 */
public final List loadEntityBatch(final SharedSessionContractImplementor session, final Serializable[] ids, final Type idType, final Object optionalObject, final String optionalEntityName, final Serializable optionalId, final EntityPersister persister, LockOptions lockOptions) throws HibernateException {
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Batch loading entity: %s", MessageHelper.infoString(persister, ids, getFactory()));
    }
    Type[] types = new Type[ids.length];
    Arrays.fill(types, idType);
    List result;
    try {
        QueryParameters qp = new QueryParameters();
        qp.setPositionalParameterTypes(types);
        qp.setPositionalParameterValues(ids);
        qp.setOptionalObject(optionalObject);
        qp.setOptionalEntityName(optionalEntityName);
        qp.setOptionalId(optionalId);
        qp.setLockOptions(lockOptions);
        result = doQueryAndInitializeNonLazyCollections(session, qp, false);
    } catch (SQLException sqle) {
        throw factory.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not load an entity batch: " + MessageHelper.infoString(getEntityPersisters()[0], ids, getFactory()), getSQLString());
    }
    LOG.debug("Done entity batch load");
    return result;
}
Also used : EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) SQLException(java.sql.SQLException) List(java.util.List) ArrayList(java.util.ArrayList) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 3 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class Loader method loadCollectionBatch.

/**
	 * Called by wrappers that batch initialize collections
	 */
public final void loadCollectionBatch(final SharedSessionContractImplementor session, final Serializable[] ids, final Type type) throws HibernateException {
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Batch loading collection: %s", MessageHelper.collectionInfoString(getCollectionPersisters()[0], ids, getFactory()));
    }
    Type[] idTypes = new Type[ids.length];
    Arrays.fill(idTypes, type);
    try {
        doQueryAndInitializeNonLazyCollections(session, new QueryParameters(idTypes, ids, ids), true);
    } catch (SQLException sqle) {
        throw factory.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not initialize a collection batch: " + MessageHelper.collectionInfoString(getCollectionPersisters()[0], ids, getFactory()), getSQLString());
    }
    LOG.debug("Done batch load");
}
Also used : EntityType(org.hibernate.type.EntityType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) SQLException(java.sql.SQLException) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 4 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class BatchingEntityLoader method buildQueryParameters.

protected QueryParameters buildQueryParameters(Serializable id, Serializable[] ids, Object optionalObject, LockOptions lockOptions) {
    Type[] types = new Type[ids.length];
    Arrays.fill(types, persister().getIdentifierType());
    QueryParameters qp = new QueryParameters();
    qp.setPositionalParameterTypes(types);
    qp.setPositionalParameterValues(ids);
    qp.setOptionalObject(optionalObject);
    qp.setOptionalEntityName(persister().getEntityName());
    qp.setOptionalId(id);
    qp.setLockOptions(lockOptions);
    return qp;
}
Also used : Type(org.hibernate.type.Type) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 5 with QueryParameters

use of org.hibernate.engine.spi.QueryParameters in project hibernate-orm by hibernate.

the class BatchingEntityLoader method doBatchLoad.

protected Object doBatchLoad(Serializable id, Loader loaderToUse, SharedSessionContractImplementor session, Serializable[] ids, Object optionalObject, LockOptions lockOptions) {
    if (log.isDebugEnabled()) {
        log.debugf("Batch loading entity: %s", MessageHelper.infoString(persister, ids, session.getFactory()));
    }
    QueryParameters qp = buildQueryParameters(id, ids, optionalObject, lockOptions);
    try {
        final List results = loaderToUse.doQueryAndInitializeNonLazyCollections(session, qp, false);
        log.debug("Done entity batch load");
        return getObjectFromList(results, id, session);
    } catch (SQLException sqle) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not load an entity batch: " + MessageHelper.infoString(persister(), ids, session.getFactory()), loaderToUse.getSQLString());
    }
}
Also used : SQLException(java.sql.SQLException) List(java.util.List) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Aggregations

QueryParameters (org.hibernate.engine.spi.QueryParameters)35 List (java.util.List)20 ArrayList (java.util.ArrayList)17 SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)10 Session (org.hibernate.Session)10 Type (org.hibernate.type.Type)10 Test (org.junit.Test)9 EntityPersister (org.hibernate.persister.entity.EntityPersister)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7 Work (org.hibernate.jdbc.Work)7 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)7 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)7 LoadQueryDetails (org.hibernate.loader.plan.exec.spi.LoadQueryDetails)7 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 AssociationType (org.hibernate.type.AssociationType)5 Serializable (java.io.Serializable)4 EntityType (org.hibernate.type.EntityType)4