use of org.hibernate.sql.ast.tree.expression.JdbcParameter in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method applyRestriction.
private void applyRestriction(QuerySpec rootQuerySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, ModelPart modelPart, int numberColumns, Consumer<JdbcParameter> jdbcParameterConsumer, LoaderSqlAstCreationState sqlAstCreationState) {
final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
final NavigablePath navigablePath = rootNavigablePath.append(modelPart.getNavigableRole().getNavigableName());
if (numberColumns == 1) {
modelPart.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final ColumnReference columnRef = (ColumnReference) sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), p -> new ColumnReference(tableReference, selection, creationContext.getSessionFactory()));
if (numberOfKeysToLoad == 1) {
final JdbcParameter jdbcParameter = new JdbcParameterImpl(selection.getJdbcMapping());
jdbcParameterConsumer.accept(jdbcParameter);
rootQuerySpec.applyPredicate(new ComparisonPredicate(columnRef, ComparisonOperator.EQUAL, jdbcParameter));
} else {
final InListPredicate predicate = new InListPredicate(columnRef);
for (int i = 0; i < numberOfKeysToLoad; i++) {
for (int j = 0; j < numberColumns; j++) {
final JdbcParameter jdbcParameter = new JdbcParameterImpl(columnRef.getJdbcMapping());
jdbcParameterConsumer.accept(jdbcParameter);
predicate.addExpression(jdbcParameter);
}
}
rootQuerySpec.applyPredicate(predicate);
}
});
} else {
final List<ColumnReference> columnReferences = new ArrayList<>(numberColumns);
modelPart.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
columnReferences.add((ColumnReference) sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), p -> new ColumnReference(tableReference, selection, creationContext.getSessionFactory())));
});
final SqlTuple tuple = new SqlTuple(columnReferences, modelPart);
final InListPredicate predicate = new InListPredicate(tuple);
for (int i = 0; i < numberOfKeysToLoad; i++) {
final List<JdbcParameter> tupleParams = new ArrayList<>(numberColumns);
for (int j = 0; j < numberColumns; j++) {
final ColumnReference columnReference = columnReferences.get(j);
final JdbcParameter jdbcParameter = new JdbcParameterImpl(columnReference.getJdbcMapping());
jdbcParameterConsumer.accept(jdbcParameter);
tupleParams.add(jdbcParameter);
}
final SqlTuple paramTuple = new SqlTuple(tupleParams, modelPart);
predicate.addExpression(paramTuple);
}
rootQuerySpec.applyPredicate(predicate);
}
}
use of org.hibernate.sql.ast.tree.expression.JdbcParameter in project hibernate-orm by hibernate.
the class SimpleNaturalIdLoader method applyNaturalIdRestriction.
@Override
protected void applyNaturalIdRestriction(Object bindValue, TableGroup rootTableGroup, Consumer<Predicate> predicateConsumer, BiConsumer<JdbcParameter, JdbcParameterBinding> jdbcParameterConsumer, LoaderSqlAstCreationState sqlAstCreationState, SharedSessionContractImplementor session) {
if (bindValue == null) {
naturalIdMapping().getAttribute().forEachSelectable((index, selectable) -> {
final Expression columnReference = resolveColumnReference(rootTableGroup, selectable, sqlAstCreationState.getSqlExpressionResolver(), session.getFactory());
predicateConsumer.accept(new NullnessPredicate(columnReference));
});
} else {
naturalIdMapping().getAttribute().breakDownJdbcValues(bindValue, (jdbcValue, jdbcValueMapping) -> {
final Expression columnReference = resolveColumnReference(rootTableGroup, jdbcValueMapping, sqlAstCreationState.getSqlExpressionResolver(), session.getFactory());
if (jdbcValue == null) {
predicateConsumer.accept(new NullnessPredicate(columnReference));
} else {
final JdbcParameter jdbcParameter = new JdbcParameterImpl(jdbcValueMapping.getJdbcMapping());
final ComparisonPredicate predicate = new ComparisonPredicate(columnReference, ComparisonOperator.EQUAL, jdbcParameter);
predicateConsumer.accept(predicate);
jdbcParameterConsumer.accept(jdbcParameter, new JdbcParameterBindingImpl(jdbcValueMapping.getJdbcMapping(), jdbcValue));
}
}, session);
}
}
use of org.hibernate.sql.ast.tree.expression.JdbcParameter in project hibernate-orm by hibernate.
the class SingleIdEntityLoaderDynamicBatch method load.
@Override
public T load(Object pkValue, Object entityInstance, LockOptions lockOptions, Boolean readOnly, SharedSessionContractImplementor session) {
final Object[] batchIds = session.getPersistenceContextInternal().getBatchFetchQueue().getBatchLoadableEntityIds(getLoadable(), pkValue, maxBatchSize);
final int numberOfIds = ArrayHelper.countNonNull(batchIds);
if (numberOfIds <= 1) {
initializeSingleIdLoaderIfNeeded(session);
final T result = singleIdLoader.load(pkValue, entityInstance, lockOptions, readOnly, session);
if (result == null) {
// There was no entity with the specified ID. Make sure the EntityKey does not remain
// in the batch to avoid including it in future batches that get executed.
BatchFetchQueueHelper.removeBatchLoadableEntityKey(pkValue, getLoadable(), session);
}
return result;
}
final Object[] idsToLoad = new Object[numberOfIds];
System.arraycopy(batchIds, 0, idsToLoad, 0, numberOfIds);
if (log.isDebugEnabled()) {
log.debugf("Batch loading entity [%s] : %s", getLoadable().getEntityName(), idsToLoad);
}
final List<JdbcParameter> jdbcParameters = new ArrayList<>();
final SelectStatement sqlAst = LoaderSelectBuilder.createSelect(getLoadable(), // null here means to select everything
null, getLoadable().getIdentifierMapping(), null, numberOfIds, session.getLoadQueryInfluencers(), lockOptions, jdbcParameters::add, session.getFactory());
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(getLoadable().getIdentifierMapping().getJdbcTypeCount());
int offset = 0;
for (int i = 0; i < numberOfIds; i++) {
offset += jdbcParameterBindings.registerParametersForEachJdbcValue(idsToLoad[i], Clause.WHERE, offset, getLoadable().getIdentifierMapping(), 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);
JdbcSelectExecutorStandardImpl.INSTANCE.list(jdbcSelect, jdbcParameterBindings, getExecutionContext(pkValue, entityInstance, readOnly, lockOptions, session, subSelectFetchableKeysHandler), RowTransformerPassThruImpl.instance(), ListResultsConsumer.UniqueSemantic.FILTER);
// noinspection ForLoopReplaceableByForEach
for (int i = 0; i < idsToLoad.length; i++) {
final Object id = idsToLoad[i];
// found or not, remove the key from the batch-fetch queye
BatchFetchQueueHelper.removeBatchLoadableEntityKey(id, getLoadable(), session);
}
final EntityKey entityKey = session.generateEntityKey(pkValue, getLoadable().getEntityPersister());
// noinspection unchecked
return (T) session.getPersistenceContext().getEntity(entityKey);
}
use of org.hibernate.sql.ast.tree.expression.JdbcParameter in project hibernate-orm by hibernate.
the class SingleIdEntityLoaderStandardImpl method createLoadPlan.
private SingleIdLoadPlan<T> createLoadPlan(LockOptions lockOptions, LoadQueryInfluencers queryInfluencers, SessionFactoryImplementor sessionFactory) {
final List<JdbcParameter> jdbcParameters = new ArrayList<>();
final SelectStatement sqlAst = LoaderSelectBuilder.createSelect(getLoadable(), // null here means to select everything
null, getLoadable().getIdentifierMapping(), null, 1, queryInfluencers, lockOptions, jdbcParameters::add, sessionFactory);
return new SingleIdLoadPlan<>((Loadable) getLoadable(), getLoadable().getIdentifierMapping(), sqlAst, jdbcParameters, lockOptions, sessionFactory);
}
use of org.hibernate.sql.ast.tree.expression.JdbcParameter in project hibernate-orm by hibernate.
the class SingleUniqueKeyEntityLoaderStandard method resolveId.
@Override
public Object resolveId(Object ukValue, 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.singletonList(entityDescriptor.getIdentifierMapping()), 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() {
@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 -> row[0], ListResultsConsumer.UniqueSemantic.FILTER);
assert list.size() == 1;
return list.get(0);
}
Aggregations