Skip to content

Commit eaeb0f3

Browse files
committed
HHH-19340 make TypedParameterValue a record
1 parent 221f17c commit eaeb0f3

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

hibernate-core/src/main/java/org/hibernate/query/TypedParameterValue.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import org.hibernate.type.BasicTypeReference;
99

10+
import java.util.Objects;
11+
1012
/**
1113
* Represents a typed argument to a query parameter.
1214
* <p>
@@ -21,7 +23,7 @@
2123
* For example:
2224
* <pre>
2325
* query.setParameter("stringNamedParam",
24-
* new TypedParameterValue(StandardBasicTypes.STRING, null))
26+
* TypedParameterValue.ofNull(StandardBasicTypes.STRING))
2527
* </pre>
2628
* <p>
2729
* Here, a "null string" argument was bound to the named parameter
@@ -38,26 +40,38 @@
3840
*
3941
* @see org.hibernate.type.StandardBasicTypes
4042
*/
41-
public final class TypedParameterValue<J> {
43+
public record TypedParameterValue<J>(BindableType<J> type, J value) {
4244

43-
private final BindableType<J> type;
44-
private final J value;
45+
public TypedParameterValue {
46+
Objects.requireNonNull( type, "type must not be null" );
47+
}
4548

46-
public TypedParameterValue(BindableType<J> type, J value) {
47-
this.type = type;
48-
this.value = value;
49+
/**
50+
* Obtain an instance with the given type and given value.
51+
*
52+
* @since 7.0
53+
*/
54+
public static <J> TypedParameterValue<J> of(BindableType<J> type, J value) {
55+
return new TypedParameterValue<>( type, value );
4956
}
5057

51-
public TypedParameterValue(BasicTypeReference<J> type, J value) {
52-
this.type = type;
53-
this.value = value;
58+
/**
59+
* Obtain an instance with the given type and a null value.
60+
*
61+
* @since 7.0
62+
*/
63+
public static <J> TypedParameterValue<J> ofNull(BindableType<J> type) {
64+
return new TypedParameterValue<>( type, null );
5465
}
5566

5667
/**
5768
* The value to bind
5869
*
5970
* @return The value to be bound
71+
*
72+
* @deprecated use {@link #value}
6073
*/
74+
@Deprecated(since = "7")
6175
public J getValue() {
6276
return value;
6377
}
@@ -66,7 +80,10 @@ public J getValue() {
6680
* The specific Hibernate type to use to bind the value.
6781
*
6882
* @return The Hibernate type to use.
83+
*
84+
* @deprecated use {@link #type}
6985
*/
86+
@Deprecated(since = "7")
7087
public BindableType<J> getType() {
7188
return type;
7289
}
@@ -75,8 +92,11 @@ public BindableType<J> getType() {
7592
* The specific Hibernate type reference to use to bind the value.
7693
*
7794
* @return The Hibernate type reference to use.
95+
*
96+
* @deprecated use {@link #type}
7897
*/
98+
@Deprecated(since = "7")
7999
public BasicTypeReference<J> getTypeReference() {
80-
return type instanceof BasicTypeReference ? (BasicTypeReference<J>) type : null;
100+
return type instanceof BasicTypeReference<J> reference ? reference : null;
81101
}
82102
}

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -773,13 +773,11 @@ private boolean multipleBinding(QueryParameter<Object> param, Object value){
773773
}
774774

775775
private <T> void setTypedParameter(String name, TypedParameterValue<T> typedValue) {
776-
final BindableType<T> type = typedValue.getType();
777-
setParameter( name, typedValue.getValue(), type != null ? type : typedValue.getTypeReference() );
776+
setParameter( name, typedValue.value(), typedValue.type() );
778777
}
779778

780779
private <T> void setTypedParameter(int position, TypedParameterValue<T> typedValue) {
781-
final BindableType<T> type = typedValue.getType();
782-
setParameter( position, typedValue.getValue(), type != null ? type : typedValue.getTypeReference() );
780+
setParameter( position, typedValue.value(), typedValue.type() );
783781
}
784782

785783
private boolean isInstance(BindableType<?> parameterType, Object value) {
@@ -889,19 +887,17 @@ public <P> CommonQueryContract setParameter(QueryParameter<P> parameter, P value
889887
public <P> CommonQueryContract setParameter(Parameter<P> parameter, P value) {
890888
if ( value instanceof TypedParameterValue<?> typedParameterValue ) {
891889
final Class<P> parameterType = parameter.getParameterType();
892-
final BindableType<?> type = typedParameterValue.getType();
893-
final BindableType<?> bindableType = type == null ? typedParameterValue.getTypeReference() : type;
894-
if ( bindableType == null ) {
890+
final BindableType<?> type = typedParameterValue.type();
891+
if ( type == null ) {
895892
throw new IllegalArgumentException( "TypedParameterValue has no type" );
896893
}
897-
if ( !parameterType.isAssignableFrom( bindableType.getBindableJavaType() ) ) {
894+
if ( !parameterType.isAssignableFrom( type.getBindableJavaType() ) ) {
898895
throw new QueryArgumentException( "Given TypedParameterValue is not assignable to given Parameter type",
899-
parameterType, typedParameterValue.getValue() );
896+
parameterType, typedParameterValue.value() );
900897
}
901898
@SuppressWarnings("unchecked") // safe, because we just checked
902899
final TypedParameterValue<P> typedValue = (TypedParameterValue<P>) value;
903-
setParameter( parameter, typedValue.getValue(),
904-
type == null ? typedValue.getTypeReference() : typedValue.getType() );
900+
setParameter( parameter, typedValue.value(), typedValue.type() );
905901
}
906902
else {
907903
locateBinding( parameter ).setBindValue( value, resolveJdbcParameterTypeIfNecessary() );

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/query/TypedValueParametersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testJpa(EntityManagerFactoryScope scope) {
8787
TagUserType.INSTANCE,
8888
scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getTypeConfiguration()
8989
);
90-
q.setParameter("tags", new TypedParameterValue<>( customType, Arrays.asList("important","business")));
90+
q.setParameter("tags", TypedParameterValue.of(customType, Arrays.asList("important","business")));
9191
}
9292
);
9393
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ public void testTypedParameterValueInParameter(SessionFactoryScope scope) {
432432
session -> {
433433
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
434434
procedureCall.registerParameter( 1, StandardBasicTypes.STRING, ParameterMode.IN );
435-
procedureCall.setParameter( 1, new TypedParameterValue( StandardBasicTypes.STRING, "test" ) );
435+
procedureCall.setParameter( 1, TypedParameterValue.of( StandardBasicTypes.STRING, "test" ) );
436436

437437
procedureCall = session.createStoredProcedureCall( "test" );
438438
procedureCall.registerParameter( "test", StandardBasicTypes.STRING, ParameterMode.IN );
439-
procedureCall.setParameter( "test", new TypedParameterValue( StandardBasicTypes.STRING, "test" ) );
439+
procedureCall.setParameter( "test", TypedParameterValue.of( StandardBasicTypes.STRING, "test" ) );
440440
}
441441
);
442442
}
@@ -449,7 +449,7 @@ public void testTypedParameterValueInParameterWithNotSpecifiedType(SessionFactor
449449
try {
450450
ProcedureCall procedureCall = session.createStoredProcedureCall( "test" );
451451
procedureCall.registerParameter( 1, StandardBasicTypes.STRING, ParameterMode.IN );
452-
procedureCall.setParameter( 1, new TypedParameterValue( StandardBasicTypes.INTEGER, 1 ) );
452+
procedureCall.setParameter( 1, TypedParameterValue.of( StandardBasicTypes.INTEGER, 1 ) );
453453
}
454454
catch (IllegalArgumentException e) {
455455
assertTrue( e.getMessage().contains( "was not of specified type" ) );

0 commit comments

Comments
 (0)