HHH-15618 Accept TypedParameterValue for procedure

This commit is contained in:
Yanming Zhou 2022-10-21 16:42:36 +08:00 committed by Christian Beikov
parent 45c7fc5e28
commit 49fbe84dde
2 changed files with 63 additions and 2 deletions

View File

@ -35,6 +35,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.procedure.NoSuchParameterException;
import org.hibernate.procedure.ParameterRegistration;
@ -66,6 +67,7 @@ import org.jboss.logging.Logger;
* Standard implementation of {@link org.hibernate.procedure.ProcedureCall}
*
* @author Steve Ebersole
* @author Yanming Zhou
*/
public class ProcedureCallImpl<R>
extends AbstractProducedQuery<R>
@ -814,13 +816,25 @@ public class ProcedureCallImpl<R>
@Override
public ProcedureCallImplementor<R> setParameter(String name, Object value) {
paramBindings.getBinding( getParameterMetadata().getQueryParameter( name ) ).setBindValue( value );
QueryParameterBinding<Object> binding = paramBindings.getBinding( getParameterMetadata().getQueryParameter( name ) );
if ( value instanceof TypedParameterValue ) {
binding.setBindValue( ( (TypedParameterValue) value ).getValue(), ( (TypedParameterValue) value ).getType() );
}
else {
binding.setBindValue( value );
}
return this;
}
@Override
public ProcedureCallImplementor<R> setParameter(int position, Object value) {
paramBindings.getBinding( getParameterMetadata().getQueryParameter( position ) ).setBindValue( value );
QueryParameterBinding<Object> binding = paramBindings.getBinding( getParameterMetadata().getQueryParameter( position ) );
if ( value instanceof TypedParameterValue ) {
binding.setBindValue( ( (TypedParameterValue) value ).getValue(), ( (TypedParameterValue) value ).getType() );
}
else {
binding.setBindValue( value );
}
return this;
}

View File

@ -29,6 +29,7 @@ import javax.persistence.ParameterMode;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.type.BigDecimalType;
import org.hibernate.type.BigIntegerType;
@ -71,6 +72,7 @@ import static org.junit.Assert.fail;
/**
* @author Vlad Mihalcea
* @author Yanming Zhou
*/
public class StoredProcedureParameterTypeTest extends BaseNonConfigCoreFunctionalTestCase {
@ -422,4 +424,49 @@ public class StoredProcedureParameterTypeTest extends BaseNonConfigCoreFunctiona
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-15618")
public void testTypedParameterValueInParameter() {
inTransaction(
session -> {
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
procedureCall.registerParameter( 1, StringType.class, ParameterMode.IN );
procedureCall.setParameter( 1, new TypedParameterValue( StringType.INSTANCE, "test" ) );
procedureCall = session.createStoredProcedureCall( "test" );
procedureCall.registerParameter( "test", StringType.class, ParameterMode.IN );
procedureCall.setParameter( "test", new TypedParameterValue( StringType.INSTANCE, "test" ) );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-15618")
public void testTypedParameterValueInParameterWithEnablePassingNulls() {
inTransaction(
session -> {
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
procedureCall.registerParameter( 1, StringType.class, ParameterMode.IN ).enablePassingNulls( true );
procedureCall.setParameter( 1, new TypedParameterValue( StringType.INSTANCE, null ) );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-15618")
public void testTypedParameterValueInParameterWithNotSpecifiedType() {
inTransaction(
session -> {
try {
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
procedureCall.registerParameter( 1, StringType.class, ParameterMode.IN );
procedureCall.setParameter( 1, new TypedParameterValue( IntegerType.INSTANCE, 1 ) );
}
catch (IllegalArgumentException e) {
assertTrue( e.getMessage().contains( "was not of specified type" ) );
}
}
);
}
}