Search in sources :

Example 1 with Callback

use of org.hibernate.sql.exec.spi.Callback in project hibernate-orm by hibernate.

the class JdbcValuesSourceProcessingStateStandardImpl method postLoad.

private void postLoad() {
    if (loadingEntityMap == null) {
        return;
    }
    final EventListenerGroup<PostLoadEventListener> listenerGroup = executionContext.getSession().getFactory().getFastSessionServices().eventListenerGroup_POST_LOAD;
    loadingEntityMap.forEach((entityKey, loadingEntityEntry) -> {
        if (postLoadEvent != null) {
            postLoadEvent.reset();
            postLoadEvent.setEntity(loadingEntityEntry.getEntityInstance()).setId(entityKey.getIdentifier()).setPersister(loadingEntityEntry.getDescriptor());
            listenerGroup.fireEventOnEachListener(postLoadEvent, PostLoadEventListener::onPostLoad);
        }
        final Callback callback = executionContext.getCallback();
        if (callback != null) {
            callback.invokeAfterLoadActions(getSession(), loadingEntityEntry.getEntityInstance(), (Loadable) loadingEntityEntry.getDescriptor());
        }
    });
    loadingEntityMap = null;
}
Also used : Callback(org.hibernate.sql.exec.spi.Callback) PostLoadEventListener(org.hibernate.event.spi.PostLoadEventListener)

Example 2 with Callback

use of org.hibernate.sql.exec.spi.Callback in project hibernate-orm by hibernate.

the class SingleIdLoadPlan method load.

public T load(Object restrictedValue, Object entityInstance, Boolean readOnly, Boolean singleResultExpected, SharedSessionContractImplementor session) {
    final int jdbcTypeCount = restrictivePart.getJdbcTypeCount();
    assert jdbcParameters.size() % jdbcTypeCount == 0;
    final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(jdbcTypeCount);
    jdbcSelect.bindFilterJdbcParameters(jdbcParameterBindings);
    int offset = 0;
    while (offset < jdbcParameters.size()) {
        offset += jdbcParameterBindings.registerParametersForEachJdbcValue(restrictedValue, Clause.WHERE, offset, restrictivePart, jdbcParameters, session);
    }
    assert offset == jdbcParameters.size();
    final QueryOptions queryOptions = new SimpleQueryOptions(lockOptions, readOnly);
    final Callback callback = new CallbackImpl();
    final List<T> list = JdbcSelectExecutorStandardImpl.INSTANCE.list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {

        @Override
        public SharedSessionContractImplementor getSession() {
            return session;
        }

        @Override
        public Object getEntityInstance() {
            return entityInstance;
        }

        @Override
        public Object getEntityId() {
            return restrictedValue;
        }

        @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;
        }
    }, getRowTransformer(), singleResultExpected ? ListResultsConsumer.UniqueSemantic.ASSERT : ListResultsConsumer.UniqueSemantic.FILTER);
    if (list.isEmpty()) {
        return null;
    }
    final T entity = list.get(0);
    if (persister != null) {
        callback.invokeAfterLoadActions(session, entity, persister);
    }
    return entity;
}
Also used : CallbackImpl(org.hibernate.sql.exec.internal.CallbackImpl) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) QueryOptions(org.hibernate.query.spi.QueryOptions) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) Callback(org.hibernate.sql.exec.spi.Callback) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) SimpleQueryOptions(org.hibernate.query.internal.SimpleQueryOptions) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 3 with Callback

use of org.hibernate.sql.exec.spi.Callback 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);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) ArrayList(java.util.ArrayList) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 4 with Callback

use of org.hibernate.sql.exec.spi.Callback in project hibernate-orm by hibernate.

the class ProcedureCallImpl method buildOutputs.

private ProcedureOutputsImpl buildOutputs() {
    // todo : going to need a very specialized Loader for this.
    // or, might be a good time to look at splitting Loader up into:
    // 1) building statement objects
    // 2) executing statement objects
    // 3) processing result sets
    // for now assume there are no resultClasses nor mappings defined..
    // TOTAL PROOF-OF-CONCEPT!!!!!!
    // todo : how to identify calls which should be in the form `{? = call procName...}` ??? (note leading param marker)
    // more than likely this will need to be a method on the native API.  I can see this as a trigger to
    // both: (1) add the `? = ` part and also (2) register a REFCURSOR parameter for DBs (Oracle, PGSQL) that
    // need it.
    final CallableStatementSupport callableStatementSupport = getSession().getJdbcServices().getJdbcEnvironment().getDialect().getCallableStatementSupport();
    this.call = callableStatementSupport.interpretCall(this);
    final Map<ProcedureParameter<?>, JdbcCallParameterRegistration> parameterRegistrations = new IdentityHashMap<>();
    final List<JdbcCallRefCursorExtractor> refCursorExtractors = new ArrayList<>();
    if (call.getFunctionReturn() != null) {
        parameterRegistrations.put(functionReturn, call.getFunctionReturn());
        final JdbcCallRefCursorExtractorImpl refCursorExtractor = call.getFunctionReturn().getRefCursorExtractor();
        if (refCursorExtractor != null) {
            refCursorExtractors.add(refCursorExtractor);
        }
    }
    final List<? extends ProcedureParameterImplementor<?>> registrations = getParameterMetadata().getRegistrationsAsList();
    final List<JdbcCallParameterRegistration> jdbcParameters = call.getParameterRegistrations();
    for (int i = 0; i < registrations.size(); i++) {
        final JdbcCallParameterRegistration jdbcCallParameterRegistration = jdbcParameters.get(i);
        parameterRegistrations.put(registrations.get(i), jdbcCallParameterRegistration);
        final JdbcCallRefCursorExtractorImpl refCursorExtractor = jdbcCallParameterRegistration.getRefCursorExtractor();
        if (refCursorExtractor != null) {
            refCursorExtractors.add(refCursorExtractor);
        }
    }
    LOG.debugf("Preparing procedure call : %s", call);
    final CallableStatement statement = (CallableStatement) getSession().getJdbcCoordinator().getStatementPreparer().prepareStatement(call.getSql(), true);
    // Register the parameter mode and type
    callableStatementSupport.registerParameters(procedureName, call, statement, parameterMetadata, getSession());
    // Apply the parameter bindings
    final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(parameterRegistrations.size());
    for (Map.Entry<ProcedureParameter<?>, JdbcCallParameterRegistration> entry : parameterRegistrations.entrySet()) {
        final JdbcCallParameterRegistration registration = entry.getValue();
        if (registration.getParameterBinder() != null) {
            final ProcedureParameter<?> parameter = entry.getKey();
            final QueryParameterBinding<?> binding = getParameterBindings().getBinding(parameter);
            if (!binding.isBound()) {
                if (parameter.getPosition() == null) {
                    throw new IllegalArgumentException("The parameter named [" + parameter + "] was not set! You need to call the setParameter method.");
                } else {
                    throw new IllegalArgumentException("The parameter at position [" + parameter + "] was not set! You need to call the setParameter method.");
                }
            }
            jdbcParameterBindings.addBinding((JdbcParameter) registration.getParameterBinder(), new JdbcParameterBindingImpl((JdbcMapping) registration.getParameterType(), binding.getBindValue()));
        }
    }
    final JdbcCallRefCursorExtractor[] extractors = refCursorExtractors.toArray(new JdbcCallRefCursorExtractor[0]);
    final ExecutionContext executionContext = new ExecutionContext() {

        private final Callback callback = new CallbackImpl();

        @Override
        public SharedSessionContractImplementor getSession() {
            return ProcedureCallImpl.this.getSession();
        }

        @Override
        public QueryOptions getQueryOptions() {
            return new QueryOptionsAdapter() {

                @Override
                public Boolean isReadOnly() {
                    return false;
                }
            };
        }

        @Override
        public String getQueryIdentifier(String sql) {
            return sql;
        }

        @Override
        public QueryParameterBindings getQueryParameterBindings() {
            return QueryParameterBindings.NO_PARAM_BINDINGS;
        }

        @Override
        public Callback getCallback() {
            return callback;
        }
    };
    try {
        int paramBindingPosition = call.getFunctionReturn() == null ? 1 : 2;
        for (JdbcParameterBinder parameterBinder : call.getParameterBinders()) {
            parameterBinder.bindParameterValue(statement, paramBindingPosition, jdbcParameterBindings, executionContext);
            paramBindingPosition++;
        }
    } catch (SQLException e) {
        throw getSession().getJdbcServices().getSqlExceptionHelper().convert(e, "Error registering CallableStatement parameters", procedureName);
    }
    return new ProcedureOutputsImpl(this, parameterRegistrations, extractors, statement);
}
Also used : ProcedureParameter(org.hibernate.query.procedure.ProcedureParameter) JdbcCallRefCursorExtractorImpl(org.hibernate.sql.exec.internal.JdbcCallRefCursorExtractorImpl) CallbackImpl(org.hibernate.sql.exec.internal.CallbackImpl) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) SQLException(java.sql.SQLException) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) JdbcCallRefCursorExtractor(org.hibernate.sql.exec.spi.JdbcCallRefCursorExtractor) CallableStatement(java.sql.CallableStatement) JdbcParameterBinder(org.hibernate.sql.exec.spi.JdbcParameterBinder) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) JdbcParameterBindingImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingImpl) CallableStatementSupport(org.hibernate.procedure.spi.CallableStatementSupport) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) JdbcCallParameterRegistration(org.hibernate.sql.exec.spi.JdbcCallParameterRegistration) QueryOptionsAdapter(org.hibernate.query.spi.QueryOptionsAdapter) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Example 5 with Callback

use of org.hibernate.sql.exec.spi.Callback in project hibernate-orm by hibernate.

the class CollectionElementLoaderByIndex method load.

public Object load(Object key, Object index, SharedSessionContractImplementor session) {
    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);
    offset += jdbcParameterBindings.registerParametersForEachJdbcValue(incrementIndexByBase(index), Clause.WHERE, offset, attributeMapping.getIndexDescriptor(), jdbcParameters, session);
    assert offset == jdbcParameters.size();
    final JdbcSelect jdbcSelect = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqlAst).translate(jdbcParameterBindings, QueryOptions.NONE);
    List<Object> list = jdbcServices.getJdbcSelectExecutor().list(jdbcSelect, jdbcParameterBindings, new ExecutionContext() {

        @Override
        public SharedSessionContractImplementor getSession() {
            return session;
        }

        @Override
        public CollectionKey getCollectionKey() {
            return null;
        }

        @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);
    if (list.isEmpty()) {
        return null;
    }
    return list.get(0);
}
Also used : JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) JdbcParameterBindingsImpl(org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) CollectionKey(org.hibernate.engine.spi.CollectionKey) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) QueryOptions(org.hibernate.query.spi.QueryOptions) SqlAstTranslatorFactory(org.hibernate.sql.ast.SqlAstTranslatorFactory) ExecutionContext(org.hibernate.sql.exec.spi.ExecutionContext) Callback(org.hibernate.sql.exec.spi.Callback) QueryParameterBindings(org.hibernate.query.spi.QueryParameterBindings) JdbcParameterBindings(org.hibernate.sql.exec.spi.JdbcParameterBindings)

Aggregations

Callback (org.hibernate.sql.exec.spi.Callback)15 ExecutionContext (org.hibernate.sql.exec.spi.ExecutionContext)14 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)12 QueryOptions (org.hibernate.query.spi.QueryOptions)12 QueryParameterBindings (org.hibernate.query.spi.QueryParameterBindings)12 JdbcParameterBindingsImpl (org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl)12 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)12 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)10 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)10 SqlAstTranslatorFactory (org.hibernate.sql.ast.SqlAstTranslatorFactory)10 JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)8 ArrayList (java.util.ArrayList)7 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)6 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)6 CallbackImpl (org.hibernate.sql.exec.internal.CallbackImpl)5 EntityKey (org.hibernate.engine.spi.EntityKey)4 SubselectFetch (org.hibernate.engine.spi.SubselectFetch)4 LoadingEntityEntry (org.hibernate.sql.results.graph.entity.LoadingEntityEntry)4 HibernateException (org.hibernate.HibernateException)3