Skip to content

Commit 137b359

Browse files
committed
HHH-15618 Accept TypedParameterValue for procedure
1 parent c6bfc29 commit 137b359

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

hibernate-core/src/main/java/org/hibernate/procedure/internal/ParameterBindImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.persistence.ParameterMode;
1010
import javax.persistence.TemporalType;
1111

12+
import org.hibernate.jpa.TypedParameterValue;
1213
import org.hibernate.procedure.ParameterBind;
1314
import org.hibernate.query.internal.BindingTypeHelper;
1415
import org.hibernate.query.procedure.internal.ProcedureParamBindings;
@@ -21,6 +22,7 @@
2122
* Implementation of the {@link ParameterBind} contract.
2223
*
2324
* @author Steve Ebersole
25+
* @author Yanming Zhou
2426
*/
2527
public class ParameterBindImpl<T> implements ParameterBind<T> {
2628
private static final Logger log = Logger.getLogger( ParameterBindImpl.class );
@@ -61,6 +63,12 @@ public boolean isBound() {
6163

6264
@Override
6365
public void setBindValue(T value) {
66+
if ( value instanceof TypedParameterValue ) {
67+
TypedParameterValue typedParameterValue = (TypedParameterValue) value;
68+
this.hibernateType = typedParameterValue.getType();
69+
value = (T) typedParameterValue.getValue();
70+
}
71+
6472
internalSetValue( value );
6573

6674
if ( value != null && hibernateType == null ) {

hibernate-core/src/test/java/org/hibernate/test/procedure/StoredProcedureParameterTypeTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.sql.rowset.serial.SerialBlob;
3030
import javax.sql.rowset.serial.SerialClob;
3131

32+
import org.hibernate.jpa.TypedParameterValue;
3233
import org.hibernate.procedure.ProcedureCall;
3334
import org.hibernate.type.BigDecimalType;
3435
import org.hibernate.type.BigIntegerType;
@@ -71,6 +72,7 @@
7172

7273
/**
7374
* @author Vlad Mihalcea
75+
* @author Yanming Zhou
7476
*/
7577
public class StoredProcedureParameterTypeTest extends BaseNonConfigCoreFunctionalTestCase {
7678

@@ -422,4 +424,45 @@ public void testStringTypeInParameterIsNullWithoutEnablePassingNulls() {
422424
}
423425
);
424426
}
427+
428+
@Test
429+
@TestForIssue(jiraKey = "HHH-15618")
430+
public void testTypedParameterValueInParameter() {
431+
inTransaction(
432+
session -> {
433+
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
434+
procedureCall.registerParameter( 1, StringType.class, ParameterMode.IN );
435+
procedureCall.setParameter( 1, new TypedParameterValue( StringType.INSTANCE, "test" ) );
436+
}
437+
);
438+
}
439+
440+
@Test
441+
@TestForIssue(jiraKey = "HHH-15618")
442+
public void testTypedParameterValueInParameterWithEnablePassingNulls() {
443+
inTransaction(
444+
session -> {
445+
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
446+
procedureCall.registerParameter( 1, StringType.class, ParameterMode.IN ).enablePassingNulls( true );
447+
procedureCall.setParameter( 1, new TypedParameterValue( StringType.INSTANCE, null ) );
448+
}
449+
);
450+
}
451+
452+
@Test
453+
@TestForIssue(jiraKey = "HHH-15618")
454+
public void testTypedParameterValueInParameterWithNotSpecifiedType() {
455+
inTransaction(
456+
session -> {
457+
try {
458+
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
459+
procedureCall.registerParameter( 1, StringType.class, ParameterMode.IN );
460+
procedureCall.setParameter( 1, new TypedParameterValue( IntegerType.INSTANCE, 1 ) );
461+
}
462+
catch (IllegalArgumentException e) {
463+
assertTrue( e.getMessage().contains( "was not of specified type" ) );
464+
}
465+
}
466+
);
467+
}
425468
}

0 commit comments

Comments
 (0)