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;
}
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;
}
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");
}
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;
}
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());
}
}
Aggregations