use of org.hibernate.query.spi.QueryOptions in project hibernate-orm by hibernate.
the class ListResultsConsumer method consume.
@Override
public List<R> consume(JdbcValues jdbcValues, SharedSessionContractImplementor session, JdbcValuesSourceProcessingOptions processingOptions, JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState, RowProcessingStateStandardImpl rowProcessingState, RowReader<R> rowReader) {
final PersistenceContext persistenceContext = session.getPersistenceContext();
final TypeConfiguration typeConfiguration = session.getTypeConfiguration();
final QueryOptions queryOptions = rowProcessingState.getQueryOptions();
RuntimeException ex = null;
try {
persistenceContext.getLoadContexts().register(jdbcValuesSourceProcessingState);
final List<R> results = new ArrayList<>();
final JavaType<R> domainResultJavaType = resolveDomainResultJavaType(rowReader.getDomainResultResultJavaType(), rowReader.getResultJavaTypes(), typeConfiguration);
final ResultHandler<R> resultHandlerToUse;
if (uniqueSemantic == UniqueSemantic.ALLOW && domainResultJavaType instanceof EntityJavaType) {
resultHandlerToUse = ListResultsConsumer::deDuplicationHandling;
} else {
resultHandlerToUse = this.resultHandler;
}
while (rowProcessingState.next()) {
final R row = rowReader.readRow(rowProcessingState, processingOptions);
resultHandlerToUse.handle(row, domainResultJavaType, results, rowProcessingState);
rowProcessingState.finishRowProcessing();
}
try {
jdbcValuesSourceProcessingState.finishUp();
} finally {
persistenceContext.getLoadContexts().deregister(jdbcValuesSourceProcessingState);
}
// noinspection unchecked
final ResultListTransformer<R> resultListTransformer = (ResultListTransformer<R>) queryOptions.getResultListTransformer();
if (resultListTransformer != null) {
return resultListTransformer.transformList(results);
}
return results;
} catch (RuntimeException e) {
ex = e;
} finally {
try {
rowReader.finishUp(jdbcValuesSourceProcessingState);
jdbcValues.finishUp(session);
persistenceContext.initializeNonLazyCollections();
} catch (RuntimeException e) {
if (ex != null) {
ex.addSuppressed(e);
} else {
ex = e;
}
} finally {
if (ex != null) {
throw ex;
}
}
}
throw new IllegalStateException("Should not reach this!");
}
use of org.hibernate.query.spi.QueryOptions in project hibernate-orm by hibernate.
the class GeneratedValuesProcessor method processGeneratedValues.
public void processGeneratedValues(Object entity, Object id, Object[] state, SharedSessionContractImplementor session) {
if (selectStatement == null) {
return;
}
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
final JdbcParameterBindings jdbcParamBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
int offset = jdbcParamBindings.registerParametersForEachJdbcValue(id, Clause.WHERE, entityDescriptor.getIdentifierMapping(), jdbcParameters, session);
assert offset == jdbcParameters.size();
final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, selectStatement).translate(jdbcParamBindings, QueryOptions.NONE);
final List<Object[]> results = session.getFactory().getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParamBindings, new ExecutionContext() {
@Override
public SharedSessionContractImplementor getSession() {
return session;
}
@Override
public QueryOptions getQueryOptions() {
return QueryOptions.NONE;
}
@Override
public String getQueryIdentifier(String sql) {
return sql;
}
@Override
public QueryParameterBindings getQueryParameterBindings() {
return QueryParameterBindings.NO_PARAM_BINDINGS;
}
@Override
public Callback getCallback() {
throw new UnsupportedMappingException("Follow-on locking not supported yet");
}
}, (row) -> row, ListResultsConsumer.UniqueSemantic.FILTER);
assert results.size() == 1;
final Object[] dbSelectionResults = results.get(0);
for (int i = 0; i < valueDescriptors.size(); i++) {
final GeneratedValueDescriptor descriptor = valueDescriptors.get(i);
final Object generatedValue = descriptor.resolver.resolveGeneratedValue(dbSelectionResults, entity, session);
state[descriptor.attribute.getStateArrayPosition()] = generatedValue;
descriptor.attribute.getAttributeMetadataAccess().resolveAttributeMetadata(entityDescriptor).getPropertyAccess().getSetter().set(entity, generatedValue);
}
}
use of org.hibernate.query.spi.QueryOptions in project hibernate-orm by hibernate.
the class SingleUniqueKeyEntityLoaderStandard method load.
@Override
public T load(Object ukValue, LockOptions lockOptions, Boolean readOnly, SharedSessionContractImplementor session) {
final SessionFactoryImplementor sessionFactory = session.getFactory();
// todo (6.0) : cache the SQL AST and JdbcParameters
final List<JdbcParameter> jdbcParameters = new ArrayList<>();
final SelectStatement sqlAst = LoaderSelectBuilder.createSelectByUniqueKey(entityDescriptor, Collections.emptyList(), uniqueKeyAttribute, null, 1, LoadQueryInfluencers.NONE, LockOptions.NONE, jdbcParameters::add, sessionFactory);
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(ukValue, Clause.WHERE, uniqueKeyAttribute, jdbcParameters, session);
assert offset == jdbcParameters.size();
final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
final List<Object> list = sessionFactory.getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {
private final Callback callback = new CallbackImpl();
@Override
public SharedSessionContractImplementor getSession() {
return session;
}
@Override
public QueryOptions getQueryOptions() {
return new QueryOptionsAdapter() {
@Override
public Boolean isReadOnly() {
return readOnly;
}
};
}
@Override
public String getQueryIdentifier(String sql) {
return sql;
}
@Override
public QueryParameterBindings getQueryParameterBindings() {
return QueryParameterBindings.NO_PARAM_BINDINGS;
}
@Override
public Callback getCallback() {
return callback;
}
}, row -> row[0], ListResultsConsumer.UniqueSemantic.FILTER);
switch(list.size()) {
case 0:
return null;
case 1:
// noinspection unchecked
return (T) list.get(0);
}
throw new HibernateException("More than one row with the given identifier was found: " + ukValue + ", for class: " + entityDescriptor.getEntityName());
}
Aggregations