use of org.hibernate.query.spi.QueryParameterBindings in project hibernate-orm by hibernate.
the class DatabaseSnapshotExecutor method loadDatabaseSnapshot.
Object[] loadDatabaseSnapshot(Object id, SharedSessionContractImplementor session) {
if (log.isTraceEnabled()) {
log.tracef("Getting current persistent state for `%s#%s`", entityDescriptor.getEntityName(), id);
}
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(entityDescriptor.getIdentifierMapping().getJdbcTypeCount());
int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(id, Clause.WHERE, entityDescriptor.getIdentifierMapping(), jdbcParameters, session);
assert offset == jdbcParameters.size();
final List<?> list = JdbcSelectExecutorStandardImpl.INSTANCE.list(jdbcSelect, jdbcParameterBindings, 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() {
return null;
}
}, RowTransformerDatabaseSnapshotImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
final int size = list.size();
assert size <= 1;
if (size == 0) {
return null;
} else {
final Object[] entitySnapshot = (Object[]) list.get(0);
// So we must exclude it from the array
if (entitySnapshot.length == 1) {
return ArrayHelper.EMPTY_OBJECT_ARRAY;
} else {
final Object[] state = new Object[entitySnapshot.length - 1];
System.arraycopy(entitySnapshot, 1, state, 0, state.length);
return state;
}
}
}
use of org.hibernate.query.spi.QueryParameterBindings in project hibernate-orm by hibernate.
the class CollectionLoaderSingleKey method load.
@Override
public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) {
final CollectionKey collectionKey = new CollectionKey(attributeMapping.getCollectionDescriptor(), key);
final SessionFactoryImplementor sessionFactory = session.getFactory();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(keyJdbcCount);
int offset = jdbcParameterBindings.registerParametersForEachJdbcValue(key, Clause.WHERE, attributeMapping.getKeyDescriptor(), jdbcParameters, session);
assert offset == jdbcParameters.size();
final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(session.getPersistenceContext().getBatchFetchQueue(), sqlAst, jdbcParameters, jdbcParameterBindings);
jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {
@Override
public SharedSessionContractImplementor getSession() {
return session;
}
@Override
public CollectionKey getCollectionKey() {
return collectionKey;
}
@Override
public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
subSelectFetchableKeysHandler.addKey(entityKey, entry);
}
@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() {
return null;
}
}, RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
return session.getPersistenceContext().getCollection(collectionKey);
}
use of org.hibernate.query.spi.QueryParameterBindings in project hibernate-orm by hibernate.
the class MultiIdLoaderStandard method loadEntitiesById.
private List<T> loadEntitiesById(List<Object> idsInBatch, LockOptions lockOptions, SharedSessionContractImplementor session) {
assert idsInBatch != null;
assert !idsInBatch.isEmpty();
final int numberOfIdsInBatch = idsInBatch.size();
if (numberOfIdsInBatch == 1) {
return performSingleMultiLoad(idsInBatch.get(0), lockOptions, session);
}
if (log.isTraceEnabled()) {
log.tracef("#loadEntitiesById(`%s`, `%s`, ..)", entityDescriptor.getEntityName(), numberOfIdsInBatch);
}
final List<JdbcParameter> jdbcParameters = new ArrayList<>(numberOfIdsInBatch * idJdbcTypeCount);
final SelectStatement sqlAst = LoaderSelectBuilder.createSelect(getLoadable(), // null here means to select everything
null, getLoadable().getIdentifierMapping(), null, numberOfIdsInBatch, session.getLoadQueryInfluencers(), lockOptions, 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 = 0;
for (int i = 0; i < numberOfIdsInBatch; i++) {
final Object id = idsInBatch.get(i);
offset += jdbcParameterBindings.registerParametersForEachJdbcValue(id, Clause.WHERE, offset, entityDescriptor.getIdentifierMapping(), jdbcParameters, session);
}
// we should have used all the JdbcParameter references (created bindings for all)
assert offset == jdbcParameters.size();
final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
final SubselectFetch.RegistrationHandler subSelectFetchableKeysHandler;
if (entityDescriptor.hasSubselectLoadableCollections()) {
subSelectFetchableKeysHandler = SubselectFetch.createRegistrationHandler(session.getPersistenceContext().getBatchFetchQueue(), sqlAst, jdbcParameters, jdbcParameterBindings);
} else {
subSelectFetchableKeysHandler = null;
}
return JdbcSelectExecutorStandardImpl.INSTANCE.list(jdbcSelect, jdbcParameterBindings, 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() {
return null;
}
@Override
public void registerLoadingEntityEntry(EntityKey entityKey, LoadingEntityEntry entry) {
if (subSelectFetchableKeysHandler != null) {
subSelectFetchableKeysHandler.addKey(entityKey, entry);
}
}
}, RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
}
use of org.hibernate.query.spi.QueryParameterBindings in project hibernate-orm by hibernate.
the class AbstractNaturalIdLoader method resolveIdToNaturalId.
@Override
public Object resolveIdToNaturalId(Object id, SharedSessionContractImplementor session) {
final SessionFactoryImplementor sessionFactory = session.getFactory();
final List<JdbcParameter> jdbcParameters = new ArrayList<>();
final SelectStatement sqlSelect = LoaderSelectBuilder.createSelect(entityDescriptor(), Collections.singletonList(naturalIdMapping()), entityDescriptor().getIdentifierMapping(), null, 1, session.getLoadQueryInfluencers(), LockOptions.NONE, jdbcParameters::add, sessionFactory);
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, sqlSelect).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 UnsupportedOperationException("Follow-on locking not supported yet");
}
}, (row) -> {
// because we select the natural-id we want to "reduce" the result
assert row.length == 1;
return row[0];
}, ListResultsConsumer.UniqueSemantic.FILTER);
if (results.isEmpty()) {
return null;
}
if (results.size() > 1) {
throw new HibernateException(String.format("Resolving id to natural-id returned more that one row : %s #%s", entityDescriptor().getEntityName(), id));
}
return results.get(0);
}
use of org.hibernate.query.spi.QueryParameterBindings in project hibernate-orm by hibernate.
the class AbstractNaturalIdLoader method selectByNaturalId.
/**
* Perform a select, restricted by natural-id, based on `domainResultProducer` and `fetchProcessor`
*/
protected <L> L selectByNaturalId(Object bindValue, NaturalIdLoadOptions options, BiFunction<TableGroup, LoaderSqlAstCreationState, DomainResult<?>> domainResultProducer, LoaderSqlAstCreationState.FetchProcessor fetchProcessor, Function<Boolean, Long> statementStartHandler, BiConsumer<Object, Long> statementCompletionHandler, SharedSessionContractImplementor session) {
final SessionFactoryImplementor sessionFactory = session.getFactory();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
final LockOptions lockOptions;
if (options.getLockOptions() != null) {
lockOptions = options.getLockOptions();
} else {
lockOptions = LockOptions.NONE;
}
final NavigablePath entityPath = new NavigablePath(entityDescriptor.getRootPathName());
final QuerySpec rootQuerySpec = new QuerySpec(true);
final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), lockOptions, fetchProcessor, true, sessionFactory);
final TableGroup rootTableGroup = entityDescriptor.createRootTableGroup(true, entityPath, null, () -> rootQuerySpec::applyPredicate, sqlAstCreationState, sessionFactory);
rootQuerySpec.getFromClause().addRoot(rootTableGroup);
sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
final DomainResult<?> domainResult = domainResultProducer.apply(rootTableGroup, sqlAstCreationState);
final SelectStatement sqlSelect = new SelectStatement(rootQuerySpec, Collections.singletonList(domainResult));
final List<JdbcParameter> jdbcParameters = new ArrayList<>(naturalIdMapping.getJdbcTypeCount());
final JdbcParameterBindings jdbcParamBindings = new JdbcParameterBindingsImpl(jdbcParameters.size());
applyNaturalIdRestriction(bindValue, rootTableGroup, rootQuerySpec::applyPredicate, (jdbcParameter, jdbcParameterBinding) -> {
jdbcParameters.add(jdbcParameter);
jdbcParamBindings.addBinding(jdbcParameter, jdbcParameterBinding);
}, sqlAstCreationState, session);
final QueryOptions queryOptions = new SimpleQueryOptions(lockOptions, false);
final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlSelect).translate(jdbcParamBindings, queryOptions);
final StatisticsImplementor statistics = sessionFactory.getStatistics();
final Long startToken = statementStartHandler.apply(statistics.isStatisticsEnabled());
// noinspection unchecked
final List<L> results = session.getFactory().getJdbcServices().getJdbcSelectExecutor().list(jdbcSelect, jdbcParamBindings, new ExecutionContext() {
private final Callback callback = new CallbackImpl();
@Override
public SharedSessionContractImplementor getSession() {
return session;
}
@Override
public QueryOptions getQueryOptions() {
return queryOptions;
}
@Override
public String getQueryIdentifier(String sql) {
return sql;
}
@Override
public QueryParameterBindings getQueryParameterBindings() {
return QueryParameterBindings.NO_PARAM_BINDINGS;
}
@Override
public Callback getCallback() {
return callback;
}
}, row -> (L) row[0], ListResultsConsumer.UniqueSemantic.FILTER);
if (results.size() > 1) {
throw new HibernateException(String.format("Loading by natural-id returned more that one row : %s", entityDescriptor.getEntityName()));
}
final L result;
if (results.isEmpty()) {
result = null;
} else {
result = results.get(0);
}
statementCompletionHandler.accept(result, startToken);
return result;
}
Aggregations