diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTableHelper.java b/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTableHelper.java index acb4268e4624..10e83ed7adf1 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTableHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/temptable/TemporaryTableHelper.java @@ -147,26 +147,23 @@ public static void cleanTemporaryTableRows( TemporaryTableExporter exporter, Function sessionUidAccess, SharedSessionContractImplementor session) { - PreparedStatement ps = null; + PreparedStatement preparedStatement = null; try { final String sql = exporter.getSqlTruncateCommand( temporaryTable, sessionUidAccess, session ); - - ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql, false ); - + preparedStatement = session.getJdbcCoordinator().getStatementPreparer().prepareStatement( sql ); if ( temporaryTable.getSessionUidColumn() != null ) { final String sessionUid = sessionUidAccess.apply( session ); - ps.setString( 1, sessionUid ); + preparedStatement.setString( 1, sessionUid ); } - - session.getJdbcCoordinator().getResultSetReturn().executeUpdate( ps, sql ); + session.getJdbcCoordinator().getResultSetReturn().executeUpdate( preparedStatement, sql ); } catch( Throwable t ) { log.unableToCleanupTemporaryIdTable(t); } finally { - if ( ps != null ) { + if ( preparedStatement != null ) { try { - session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( ps ); + session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( preparedStatement ); } catch( Throwable ignore ) { // ignore diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java index ccc232e8e8cf..34a1a4712512 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/StatementPreparerImpl.java @@ -4,6 +4,7 @@ */ package org.hibernate.engine.jdbc.internal; +import java.sql.CallableStatement; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -76,7 +77,7 @@ public Statement createStatement() { @Override public PreparedStatement prepareStatement(String sql) { - return buildPreparedStatementPreparationTemplate( sql, false ).prepareStatement(); + return prepareStatement( sql, false ); } @Override @@ -85,6 +86,12 @@ public PreparedStatement prepareStatement(String sql, final boolean isCallable) return buildPreparedStatementPreparationTemplate( sql, isCallable ).prepareStatement(); } + @Override + public CallableStatement prepareCallableStatement(String sql) { + jdbcCoordinator.executeBatch(); + return (CallableStatement) prepareStatement( sql, true ); + } + private StatementPreparationTemplate buildPreparedStatementPreparationTemplate(String sql, final boolean isCallable) { return new StatementPreparationTemplate( sql ) { @Override diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorStandard.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorStandard.java index 25de13e4eb88..44fd5906c3d3 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorStandard.java @@ -70,9 +70,9 @@ public MutationExecutorStandard( int batchSize, SharedSessionContractImplementor session) { this.mutationOperationGroup = mutationOperationGroup; - this.generatedValuesDelegate = mutationOperationGroup.asEntityMutationOperationGroup() != null ? - mutationOperationGroup.asEntityMutationOperationGroup().getMutationDelegate() : - null; + this.generatedValuesDelegate = mutationOperationGroup.asEntityMutationOperationGroup() != null + ? mutationOperationGroup.asEntityMutationOperationGroup().getMutationDelegate() + : null; final BatchKey batchKey = batchKeySupplier.getBatchKey(); @@ -88,11 +88,11 @@ public MutationExecutorStandard( for ( int i = mutationOperationGroup.getNumberOfOperations() - 1; i >= 0; i-- ) { final MutationOperation operation = mutationOperationGroup.getOperation( i ); - if ( operation instanceof SelfExecutingUpdateOperation ) { + if ( operation instanceof SelfExecutingUpdateOperation selfExecutingUpdateOperation ) { if ( selfExecutingMutations == null ) { selfExecutingMutations = new ArrayList<>(); } - selfExecutingMutations.add( 0, ( (SelfExecutingUpdateOperation) operation ) ); + selfExecutingMutations.add( 0, selfExecutingUpdateOperation ); } else { final PreparableMutationOperation preparableMutationOperation = (PreparableMutationOperation) operation; @@ -245,9 +245,11 @@ protected GeneratedValues performNonBatchedOperations( session ); - final Object id = entityGroup.getMutationType() == MutationType.INSERT && details.getMutatingTableDetails().isIdentifierTable() ? - generatedValues.getGeneratedValue( entityTarget.getTargetPart().getIdentifierMapping() ) : - null; + final Object id = + entityGroup.getMutationType() == MutationType.INSERT + && details.getMutatingTableDetails().isIdentifierTable() + ? generatedValues.getGeneratedValue( entityTarget.getTargetPart().getIdentifierMapping() ) + : null; nonBatchedStatementGroup.forEachStatement( (tableName, statementDetails) -> { if ( !statementDetails.getMutatingTableDetails().isIdentifierTable() ) { performNonBatchedMutation( diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/StatementPreparer.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/StatementPreparer.java index ff94dfdf64dd..bfdec32cac31 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/StatementPreparer.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/spi/StatementPreparer.java @@ -4,6 +4,7 @@ */ package org.hibernate.engine.jdbc.spi; +import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.Statement; @@ -37,6 +38,17 @@ public interface StatementPreparer { */ PreparedStatement prepareStatement(String sql); + /** + * Prepare a statement. + * + * @param sql The SQL the statement to be prepared + * + * @return the prepared statement + * + * @since 7.0 + */ + CallableStatement prepareCallableStatement(String sql); + /** * Prepare a statement. * diff --git a/hibernate-core/src/main/java/org/hibernate/generator/internal/SourceGeneration.java b/hibernate-core/src/main/java/org/hibernate/generator/internal/SourceGeneration.java index a46e970f3ba7..577fa62e8055 100644 --- a/hibernate-core/src/main/java/org/hibernate/generator/internal/SourceGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/generator/internal/SourceGeneration.java @@ -9,6 +9,7 @@ import org.hibernate.annotations.SourceType; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.spi.JdbcCoordinator; +import org.hibernate.engine.jdbc.spi.StatementPreparer; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.generator.EventType; import org.hibernate.generator.EventTypeSets; @@ -79,23 +80,21 @@ public EnumSet getEventTypes() { @Override public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) { - if ( valueGenerator == null ) { - return propertyType.wrap( getCurrentTimestamp( session ), session ); - } - else { - return valueGenerator.generate(); - } + return valueGenerator == null + ? propertyType.wrap( getCurrentTimestamp( session ), session ) + : valueGenerator.generate(); } private Timestamp getCurrentTimestamp(SharedSessionContractImplementor session) { - Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect(); - boolean callable = dialect.isCurrentTimestampSelectStringCallable(); - String timestampSelectString = dialect.getCurrentTimestampSelectString(); + final Dialect dialect = session.getJdbcServices().getJdbcEnvironment().getDialect(); + final boolean callable = dialect.isCurrentTimestampSelectStringCallable(); + final String timestampSelectString = dialect.getCurrentTimestampSelectString(); + final JdbcCoordinator coordinator = session.getJdbcCoordinator(); + final StatementPreparer statementPreparer = coordinator.getStatementPreparer(); PreparedStatement statement = null; - JdbcCoordinator coordinator = session.getJdbcCoordinator(); try { - statement = prepareStatement( coordinator, timestampSelectString, callable ); - Timestamp ts = callable + statement = statementPreparer.prepareStatement( timestampSelectString, callable ); + final Timestamp ts = callable ? extractCalledResult( statement, coordinator, timestampSelectString ) : extractResult( statement, coordinator, timestampSelectString ); logResult( ts ); @@ -116,23 +115,16 @@ private Timestamp getCurrentTimestamp(SharedSessionContractImplementor session) } } - private static PreparedStatement prepareStatement( - JdbcCoordinator coordinator, - String timestampSelectString, - boolean callable) { - return coordinator.getStatementPreparer().prepareStatement( timestampSelectString, callable ); - } - private static Timestamp extractResult(PreparedStatement statement, JdbcCoordinator coordinator, String sql) throws SQLException { - ResultSet resultSet = coordinator.getResultSetReturn().extract( statement, sql ); + final ResultSet resultSet = coordinator.getResultSetReturn().extract( statement, sql ); resultSet.next(); return resultSet.getTimestamp( 1 ); } private static Timestamp extractCalledResult(PreparedStatement statement, JdbcCoordinator coordinator, String sql) throws SQLException { - CallableStatement callable = (CallableStatement) statement; + final CallableStatement callable = (CallableStatement) statement; callable.registerOutParameter( 1, TIMESTAMP ); coordinator.getResultSetReturn().execute( callable, sql ); return callable.getTimestamp( 1 ); diff --git a/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java b/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java index a6a7dd399f95..ebcf3362eebf 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java @@ -149,10 +149,10 @@ public final GeneratedValues performInsertReturning(String sql, SharedSessionCon try { //fetch the generated id in a separate query - PreparedStatement idSelect = statementPreparer.prepareStatement( selectSQL, false ); + final PreparedStatement idSelect = statementPreparer.prepareStatement( selectSQL ); try { bindParameters( binder.getEntity(), idSelect, session ); - ResultSet resultSet = jdbcCoordinator.getResultSetReturn().extract( idSelect, selectSQL ); + final ResultSet resultSet = jdbcCoordinator.getResultSetReturn().extract( idSelect, selectSQL ); try { return extractReturningValues( resultSet, session ); } diff --git a/hibernate-core/src/main/java/org/hibernate/procedure/ProcedureCall.java b/hibernate-core/src/main/java/org/hibernate/procedure/ProcedureCall.java index cbded73feb59..733194a04f53 100644 --- a/hibernate-core/src/main/java/org/hibernate/procedure/ProcedureCall.java +++ b/hibernate-core/src/main/java/org/hibernate/procedure/ProcedureCall.java @@ -155,7 +155,7 @@ public interface ProcedureCall * @throws ParameterStrategyException If the ProcedureCall is defined using named parameters * @throws NoSuchParameterException If no parameter with that position exists */ - ProcedureParameter getParameterRegistration(int position); + ProcedureParameter getParameterRegistration(int position); /** * Basic form for registering a named parameter. @@ -205,14 +205,14 @@ ProcedureParameter registerParameter(String parameterName, BasicTypeRefer * @throws ParameterStrategyException If the ProcedureCall is defined using positional parameters * @throws NoSuchParameterException If no parameter with that name exists */ - ProcedureParameter getParameterRegistration(String name); + ProcedureParameter getParameterRegistration(String name); /** * Retrieve all registered parameters. * * @return The (immutable) list of all registered parameters. */ - List getRegisteredParameters(); + List> getRegisteredParameters(); /** * Retrieves access to outputs of this procedure call. Can be called multiple times, returning the same @@ -243,8 +243,8 @@ default void close() { @Override ProcedureCall addSynchronizedEntityName(String entityName) throws MappingException; - @Override @SuppressWarnings("rawtypes") - ProcedureCall addSynchronizedEntityClass(Class entityClass) throws MappingException; + @Override + ProcedureCall addSynchronizedEntityClass(@SuppressWarnings("rawtypes") Class entityClass) throws MappingException; @Override NamedCallableQueryMemento toMemento(String name); @@ -283,8 +283,8 @@ default void close() { ProcedureCall setFlushMode(FlushModeType flushMode); @Override - ProcedureCall registerStoredProcedureParameter(int position, Class type, ParameterMode mode); + ProcedureCall registerStoredProcedureParameter(int position, Class type, ParameterMode mode); @Override - ProcedureCall registerStoredProcedureParameter(String parameterName, Class type, ParameterMode mode); + ProcedureCall registerStoredProcedureParameter(String parameterName, Class type, ParameterMode mode); } diff --git a/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java b/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java index 56eba3c57d11..c2935970980a 100644 --- a/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java @@ -10,7 +10,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; -import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.IdentityHashMap; @@ -23,6 +22,8 @@ import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.ScrollMode; +import org.hibernate.engine.jdbc.spi.JdbcCoordinator; +import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.graph.GraphSemantic; import org.hibernate.graph.RootGraph; @@ -57,7 +58,6 @@ import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.query.spi.ScrollableResultsImplementor; import org.hibernate.query.sqm.SqmExpressible; -import org.hibernate.result.NoMoreReturnsException; import org.hibernate.result.Output; import org.hibernate.result.ResultSetOutput; import org.hibernate.result.UpdateCountOutput; @@ -92,6 +92,9 @@ import static java.lang.Boolean.parseBoolean; import static java.util.Collections.emptyList; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableList; +import static java.util.Collections.unmodifiableSet; import static org.hibernate.internal.util.StringHelper.join; import static org.hibernate.jpa.HibernateHints.HINT_CALLABLE_FUNCTION; import static org.hibernate.procedure.internal.NamedCallableQueryMementoImpl.ParameterMementoImpl.fromRegistration; @@ -311,8 +314,7 @@ private void applyCallableFunctionHint() { ); if ( resultTypes.size() == 1 ) { final BasicType type = - getSessionFactory().getTypeConfiguration() - .getBasicTypeForJavaType( resultTypes.get(0) ); + getTypeConfiguration().getBasicTypeForJavaType( resultTypes.get(0) ); if ( type != null ) { markAsFunctionCall( type ); } @@ -369,10 +371,13 @@ private void markAsFunctionCallRefRefCursor() { functionReturn = new FunctionReturnImpl<>( this, Types.REF_CURSOR ); } + private TypeConfiguration getTypeConfiguration() { + return getSessionFactory().getTypeConfiguration(); + } + @Override public ProcedureCallImpl markAsFunctionCall(Class resultType) { - final TypeConfiguration typeConfiguration = getSessionFactory().getTypeConfiguration(); - final BasicType basicType = typeConfiguration.getBasicTypeForJavaType( resultType ); + final BasicType basicType = getTypeConfiguration().getBasicTypeForJavaType( resultType ); if ( basicType == null ) { throw new IllegalArgumentException( "Could not resolve a BasicType for the java type: " + resultType.getName() ); } @@ -382,9 +387,8 @@ public ProcedureCallImpl markAsFunctionCall(Class resultType) { @Override public ProcedureCallImpl markAsFunctionCall(BasicTypeReference typeReference) { - final BasicType basicType = getSessionFactory().getTypeConfiguration() - .getBasicTypeRegistry() - .resolve( typeReference ); + final BasicType basicType = + getTypeConfiguration().getBasicTypeRegistry().resolve( typeReference ); if ( basicType == null ) { throw new IllegalArgumentException( "Could not resolve a BasicType for the java type: " + typeReference.getName() ); } @@ -396,9 +400,7 @@ private void markAsFunctionCall(BasicType basicType) { if ( resultSetMapping.getNumberOfResultBuilders() == 0 ) { // Function returns might not be represented as callable parameters, // but we still want to convert the result to the requested java type if possible - resultSetMapping.addResultBuilder( - new ScalarDomainResultBuilder<>( basicType.getExpressibleJavaType() ) - ); + resultSetMapping.addResultBuilder( new ScalarDomainResultBuilder<>( basicType.getExpressibleJavaType() ) ); } //noinspection unchecked functionReturn = new FunctionReturnImpl<>( this, (OutputableType) basicType ); @@ -424,10 +426,8 @@ public Query setOrder(Order order) { // Parameter registrations @Override - @SuppressWarnings("unchecked") - public ProcedureCallImplementor registerStoredProcedureParameter(int position, Class type, ParameterMode mode) { + public ProcedureCallImplementor registerStoredProcedureParameter(int position, Class type, ParameterMode mode) { getSession().checkOpen( true ); - try { registerParameter( position, type, mode ); } @@ -438,15 +438,13 @@ public ProcedureCallImplementor registerStoredProcedureParameter(int position getSession().markForRollbackOnly(); throw e; } - return this; } @Override - @SuppressWarnings("unchecked") public ProcedureCallImplementor registerStoredProcedureParameter( String parameterName, - Class type, + Class type, ParameterMode mode) { getSession().checkOpen( true ); try { @@ -459,7 +457,6 @@ public ProcedureCallImplementor registerStoredProcedureParameter( getSession().markForRollbackOnly(); throw e; } - return this; } @@ -480,7 +477,6 @@ public ProcedureCallImplementor registerStoredProcedureParameter( getSession().markForRollbackOnly(); throw e; } - return this; } @@ -500,7 +496,6 @@ public ProcedureCallImplementor registerStoredProcedureParameter( getSession().markForRollbackOnly(); throw e; } - return this; } @@ -508,12 +503,8 @@ public ProcedureCallImplementor registerStoredProcedureParameter( public ProcedureParameter registerParameter(int position, Class javaType, ParameterMode mode) { final BindableType parameterType = getSessionFactory().getMappingMetamodel().resolveParameterBindType( javaType ); - final ProcedureParameterImpl procedureParameter = new ProcedureParameterImpl<>( - position, - mode, - getExpressibleJavaType( parameterType ), - parameterType - ); + final ProcedureParameterImpl procedureParameter = + new ProcedureParameterImpl<>( position, mode, getExpressibleJavaType( parameterType ), parameterType ); registerParameter( procedureParameter ); return procedureParameter; } @@ -523,15 +514,10 @@ public ProcedureParameter registerParameter( int position, BasicTypeReference typeReference, ParameterMode mode) { - final BasicType basicType = getSessionFactory().getTypeConfiguration() - .getBasicTypeRegistry() - .resolve( typeReference ); - final ProcedureParameterImpl procedureParameter = new ProcedureParameterImpl<>( - position, - mode, - basicType.getJavaType(), - basicType - ); + final BasicType basicType = + getTypeConfiguration().getBasicTypeRegistry().resolve( typeReference ); + final ProcedureParameterImpl procedureParameter = + new ProcedureParameterImpl<>( position, mode, basicType.getJavaType(), basicType ); registerParameter( procedureParameter ); return procedureParameter; } @@ -549,12 +535,8 @@ public ProcedureParameterImplementor getParameterRegistration(int position) { public ProcedureParameterImplementor registerParameter(String name, Class javaType, ParameterMode mode) { final BindableType parameterType = getSessionFactory().getMappingMetamodel().resolveParameterBindType( javaType ); - final ProcedureParameterImpl parameter = new ProcedureParameterImpl<>( - name, - mode, - getExpressibleJavaType( parameterType ), - parameterType - ); + final ProcedureParameterImpl parameter = + new ProcedureParameterImpl<>( name, mode, getExpressibleJavaType( parameterType ), parameterType ); registerParameter( parameter ); return parameter; } @@ -567,7 +549,6 @@ private Class getExpressibleJavaType(BindableType parameterType) { final SqmExpressible sqmExpressible = parameterType.resolveExpressible( getSessionFactory().getQueryEngine().getCriteriaBuilder() ); assert sqmExpressible != null; - return sqmExpressible.getExpressibleJavaType().getJavaTypeClass(); } } @@ -577,18 +558,11 @@ public ProcedureParameterImplementor registerParameter( String name, BasicTypeReference typeReference, ParameterMode mode) { - final BasicType basicType = getSessionFactory().getTypeConfiguration() - .getBasicTypeRegistry() - .resolve( typeReference ); - final ProcedureParameterImpl parameter = new ProcedureParameterImpl<>( - name, - mode, - basicType.getJavaType(), - basicType - ); - + final BasicType basicType = + getTypeConfiguration().getBasicTypeRegistry().resolve( typeReference ); + final ProcedureParameterImpl parameter = + new ProcedureParameterImpl<>( name, mode, basicType.getJavaType(), basicType ); registerParameter( parameter ); - return parameter; } @@ -598,9 +572,8 @@ public ProcedureParameterImplementor getParameterRegistration(String name) { } @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) - public List getRegisteredParameters() { - return getParameterMetadata().getRegistrationsAsList() ; + public List> getRegisteredParameters() { + return unmodifiableList( getParameterMetadata().getRegistrationsAsList() ); } @Override @@ -608,7 +581,6 @@ public ProcedureOutputs getOutputs() { if ( outputs == null ) { outputs = buildOutputs(); } - return outputs; } @@ -619,7 +591,7 @@ private ProcedureOutputsImpl buildOutputs() { // 2) executing statement objects // 3) processing result sets - // for now assume there are no resultClasses nor mappings defined.. + // 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) @@ -627,10 +599,10 @@ private ProcedureOutputsImpl buildOutputs() { // 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(); + final JdbcServices jdbcServices = getSession().getJdbcServices(); + + final CallableStatementSupport callableStatementSupport = + jdbcServices.getJdbcEnvironment().getDialect().getCallableStatementSupport(); final JdbcOperationQueryCall call = callableStatementSupport.interpretCall( this ); final Map, JdbcCallParameterRegistration> parameterRegistrations = new IdentityHashMap<>(); @@ -642,22 +614,24 @@ private ProcedureOutputsImpl buildOutputs() { refCursorExtractors.add( refCursorExtractor ); } } - final List> registrations = getParameterMetadata().getRegistrationsAsList(); + final List> registrations = + getParameterMetadata().getRegistrationsAsList(); final List 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(); + final JdbcCallRefCursorExtractorImpl refCursorExtractor = + jdbcCallParameterRegistration.getRefCursorExtractor(); if ( refCursorExtractor != null ) { refCursorExtractors.add( refCursorExtractor ); } } + final JdbcCoordinator jdbcCoordinator = getSession().getJdbcCoordinator(); + final String sqlString = call.getSqlString(); - final CallableStatement statement = (CallableStatement) getSession() - .getJdbcCoordinator() - .getStatementPreparer() - .prepareStatement( sqlString, true ); + final CallableStatement statement = + jdbcCoordinator.getStatementPreparer().prepareCallableStatement( sqlString ); try { // Register the parameter mode and type callableStatementSupport.registerParameters( @@ -669,8 +643,9 @@ private ProcedureOutputsImpl buildOutputs() { ); // Apply the parameter bindings - final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl( parameterRegistrations.size() ); - for ( Map.Entry, JdbcCallParameterRegistration> entry : parameterRegistrations.entrySet() ) { + final JdbcParameterBindings jdbcParameterBindings = + new JdbcParameterBindingsImpl( parameterRegistrations.size() ); + for ( var entry : parameterRegistrations.entrySet() ) { final JdbcCallParameterRegistration registration = entry.getValue(); if ( registration.getParameterBinder() != null ) { final ProcedureParameter parameter = entry.getKey(); @@ -684,12 +659,11 @@ private ProcedureOutputsImpl buildOutputs() { } } final JdbcMapping parameterType = (JdbcMapping) registration.getParameterType(); - final Object bindValue = binding.getBindValue(); jdbcParameterBindings.addBinding( (JdbcParameter) registration.getParameterBinder(), new JdbcParameterBindingImpl( parameterType, - parameterType.convertToRelationalValue( bindValue ) + parameterType.convertToRelationalValue( binding.getBindValue() ) ) ); } @@ -711,8 +685,8 @@ private ProcedureOutputsImpl buildOutputs() { } } catch (SQLException e) { - getSession().getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( statement ); - throw getSession().getJdbcServices().getSqlExceptionHelper().convert( + jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( statement ); + throw jdbcServices.getSqlExceptionHelper().convert( e, "Error registering CallableStatement parameters", procedureName @@ -749,12 +723,7 @@ protected Set synchronizedQuerySpaces() { @Override public Set getSynchronizedQuerySpaces() { - if ( synchronizedQuerySpaces == null ) { - return Collections.emptySet(); - } - else { - return Collections.unmodifiableSet( synchronizedQuerySpaces ); - } + return synchronizedQuerySpaces == null ? emptySet() : unmodifiableSet( synchronizedQuerySpaces ); } @Override @@ -945,27 +914,29 @@ protected List doList() { if ( getMaxResults() == 0 ) { return emptyList(); } - try { - final Output rtn = outputs().getCurrent(); - if ( !(rtn instanceof ResultSetOutput) ) { - throw new IllegalStateException( "Current CallableStatement was not a ResultSet, but getResultList was called" ); + else { + try { + if ( outputs().getCurrent() instanceof ResultSetOutput resultSetOutput ) { + return ((ResultSetOutput) resultSetOutput).getResultList(); + } + else { + throw new IllegalStateException( + "Current CallableStatement was not a ResultSet, but getResultList was called" ); + } + } + catch (NoMoreOutputsException e) { + // todo : the spec is completely silent on these type of edge-case scenarios. + // Essentially here we'd have a case where there are no more results (ResultSets nor updateCount) but + // getResultList was called. + return null; + } + catch (HibernateException he) { + throw getSession().getExceptionConverter().convert( he ); + } + catch (RuntimeException e) { + getSession().markForRollbackOnly(); + throw e; } - - //noinspection unchecked - return ( (ResultSetOutput) rtn ).getResultList(); - } - catch (NoMoreOutputsException e) { - // todo : the spec is completely silent on these type of edge-case scenarios. - // Essentially here we'd have a case where there are no more results (ResultSets nor updateCount) but - // getResultList was called. - return null; - } - catch (HibernateException he) { - throw getSession().getExceptionConverter().convert( he ); - } - catch (RuntimeException e) { - getSession().markForRollbackOnly(); - throw e; } } @@ -990,32 +961,8 @@ protected ScrollableResultsImplementor doScroll(ScrollMode scrollMode) { } @Override - @SuppressWarnings("unchecked") public List getResultList() { - if ( getMaxResults() == 0 ) { - return Collections.EMPTY_LIST; - } - try { - final Output rtn = outputs().getCurrent(); - if ( !( rtn instanceof ResultSetOutput ) ) { - throw new IllegalStateException( "Current CallableStatement ou was not a ResultSet, but getResultList was called" ); - } - - return ( (ResultSetOutput) rtn ).getResultList(); - } - catch (NoMoreReturnsException e) { - // todo : the spec is completely silent on these type of edge-case scenarios. - // Essentially here we'd have a case where there are no more results (ResultSets nor updateCount) but - // getResultList was called. - return null; - } - catch (HibernateException he) { - throw getSession().getExceptionConverter().convert( he ); - } - catch (RuntimeException e) { - getSession().markForRollbackOnly(); - throw e; - } + return doList(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/procedure/spi/ProcedureCallImplementor.java b/hibernate-core/src/main/java/org/hibernate/procedure/spi/ProcedureCallImplementor.java index 603132d25dcc..17ec506dec18 100644 --- a/hibernate-core/src/main/java/org/hibernate/procedure/spi/ProcedureCallImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/procedure/spi/ProcedureCallImplementor.java @@ -31,7 +31,7 @@ default List getResultList() { ParameterStrategy getParameterStrategy(); - FunctionReturnImplementor getFunctionReturn(); + FunctionReturnImplementor getFunctionReturn(); @Override ProcedureParameterMetadataImplementor getParameterMetadata(); @@ -88,9 +88,9 @@ default List getResultList() { ProcedureCallImplementor setTimeout(Integer timeout); @Override - ProcedureCallImplementor registerStoredProcedureParameter(int position, Class type, ParameterMode mode); + ProcedureCallImplementor registerStoredProcedureParameter(int position, Class type, ParameterMode mode); @Override - ProcedureCallImplementor registerStoredProcedureParameter(String parameterName, Class type, ParameterMode mode); + ProcedureCallImplementor registerStoredProcedureParameter(String parameterName, Class type, ParameterMode mode); } diff --git a/hibernate-core/src/main/java/org/hibernate/result/Output.java b/hibernate-core/src/main/java/org/hibernate/result/Output.java index 7c51e890b061..12c6ea3215b1 100644 --- a/hibernate-core/src/main/java/org/hibernate/result/Output.java +++ b/hibernate-core/src/main/java/org/hibernate/result/Output.java @@ -15,7 +15,7 @@ public interface Output { * Determine if this return is a result (castable to {@link ResultSetOutput}). The alternative is that it is * an update count (castable to {@link UpdateCountOutput}). * - * @return {@code true} indicates that {@code this} can be safely cast to {@link ResultSetOutput}), other wise + * @return {@code true} indicates that {@code this} can be safely cast to {@link ResultSetOutput}), otherwise * it can be cast to {@link UpdateCountOutput}. */ boolean isResultSet(); diff --git a/hibernate-core/src/main/java/org/hibernate/result/ResultSetOutput.java b/hibernate-core/src/main/java/org/hibernate/result/ResultSetOutput.java index ae28512ddfa3..65b523caea60 100644 --- a/hibernate-core/src/main/java/org/hibernate/result/ResultSetOutput.java +++ b/hibernate-core/src/main/java/org/hibernate/result/ResultSetOutput.java @@ -11,13 +11,13 @@ * * @author Steve Ebersole */ -public interface ResultSetOutput extends Output { +public interface ResultSetOutput extends Output { /** * Consume the underlying {@link java.sql.ResultSet} and return the resulting List. * * @return The consumed ResultSet values. */ - List getResultList(); + List getResultList(); /** * Consume the underlying {@link java.sql.ResultSet} with the expectation that there is just a single level of diff --git a/hibernate-core/src/main/java/org/hibernate/result/internal/ResultSetOutputImpl.java b/hibernate-core/src/main/java/org/hibernate/result/internal/ResultSetOutputImpl.java index 8dd917d12ee6..7eb9daebd362 100644 --- a/hibernate-core/src/main/java/org/hibernate/result/internal/ResultSetOutputImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/result/internal/ResultSetOutputImpl.java @@ -14,14 +14,14 @@ * * @author Steve Ebersole */ -class ResultSetOutputImpl implements ResultSetOutput { - private final Supplier> resultSetSupplier; +class ResultSetOutputImpl implements ResultSetOutput { + private final Supplier> resultSetSupplier; - public ResultSetOutputImpl(List results) { + public ResultSetOutputImpl(List results) { this.resultSetSupplier = () -> results; } - public ResultSetOutputImpl(Supplier> resultSetSupplier) { + public ResultSetOutputImpl(Supplier> resultSetSupplier) { this.resultSetSupplier = resultSetSupplier; } @@ -31,18 +31,13 @@ public boolean isResultSet() { } @Override - public List getResultList() { + public List getResultList() { return resultSetSupplier.get(); } @Override public Object getSingleResult() { final List results = getResultList(); - if ( results == null || results.isEmpty() ) { - return null; - } - else { - return results.get( 0 ); - } + return results == null || results.isEmpty() ? null : results.get( 0 ); } }