Skip to content

Commit 6ec67a6

Browse files
committed
improve impls of Query.unwrap()
1 parent c73c1c9 commit 6ec67a6

File tree

3 files changed

+61
-55
lines changed

3 files changed

+61
-55
lines changed

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class ProcedureCallImpl<R>
114114
private FunctionReturnImpl<R> functionReturn;
115115

116116
private final ProcedureParameterMetadataImpl parameterMetadata;
117-
private final ProcedureParamBindings paramBindings;
117+
private final ProcedureParamBindings parameterBindings;
118118

119119
private final ResultSetMapping resultSetMapping;
120120

@@ -136,7 +136,7 @@ public ProcedureCallImpl(SharedSessionContractImplementor session, String proced
136136
this.procedureName = procedureName;
137137

138138
this.parameterMetadata = new ProcedureParameterMetadataImpl();
139-
this.paramBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
139+
this.parameterBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
140140

141141
this.resultSetMapping = resolveResultSetMapping( procedureName, true, session.getSessionFactory() );
142142

@@ -158,7 +158,7 @@ public ProcedureCallImpl(SharedSessionContractImplementor session, String proced
158158
this.procedureName = procedureName;
159159

160160
this.parameterMetadata = new ProcedureParameterMetadataImpl();
161-
this.paramBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
161+
this.parameterBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
162162

163163
this.synchronizedQuerySpaces = new HashSet<>();
164164

@@ -192,7 +192,7 @@ public ProcedureCallImpl(
192192
this.procedureName = procedureName;
193193

194194
this.parameterMetadata = new ProcedureParameterMetadataImpl();
195-
this.paramBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
195+
this.parameterBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
196196

197197
this.synchronizedQuerySpaces = new HashSet<>();
198198

@@ -219,7 +219,7 @@ public ProcedureCallImpl(
219219
this.procedureName = memento.getCallableName();
220220

221221
this.parameterMetadata = new ProcedureParameterMetadataImpl( memento, session );
222-
this.paramBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
222+
this.parameterBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
223223

224224
this.synchronizedQuerySpaces = CollectionHelper.makeCopy( memento.getQuerySpaces() );
225225

@@ -251,7 +251,7 @@ public ProcedureCallImpl(
251251
this.procedureName = memento.getCallableName();
252252

253253
this.parameterMetadata = new ProcedureParameterMetadataImpl( memento, session );
254-
this.paramBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
254+
this.parameterBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
255255

256256
this.synchronizedQuerySpaces = CollectionHelper.makeCopy( memento.getQuerySpaces() );
257257

@@ -278,7 +278,7 @@ public ProcedureCallImpl(
278278
this.procedureName = memento.getCallableName();
279279

280280
this.parameterMetadata = new ProcedureParameterMetadataImpl( memento, session );
281-
this.paramBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
281+
this.parameterBindings = new ProcedureParamBindings( parameterMetadata, getSessionFactory() );
282282

283283
this.synchronizedQuerySpaces = CollectionHelper.makeCopy( memento.getQuerySpaces() );
284284

@@ -344,7 +344,7 @@ public ProcedureParameterMetadataImpl getParameterMetadata() {
344344

345345
@Override
346346
public QueryParameterBindings getQueryParameterBindings() {
347-
return paramBindings;
347+
return parameterBindings;
348348
}
349349

350350
public ParameterStrategy getParameterStrategy() {
@@ -408,7 +408,7 @@ private void markAsFunctionCall(BasicType<?> basicType) {
408408

409409
@Override
410410
public QueryParameterBindings getParameterBindings() {
411-
return paramBindings;
411+
return parameterBindings;
412412
}
413413

414414
@Override
@@ -1007,33 +1007,36 @@ else if ( resultList.size() > 1 ) {
10071007
}
10081008

10091009
@Override
1010-
@SuppressWarnings("unchecked")
1011-
public <T> T unwrap(Class<T> cls) {
1012-
if ( cls.isInstance( this ) ) {
1013-
return (T) this;
1010+
public <T> T unwrap(Class<T> type) {
1011+
if ( type.isInstance( this ) ) {
1012+
return type.cast( this );
10141013
}
10151014

1016-
if ( cls.isInstance( parameterMetadata ) ) {
1017-
return (T) parameterMetadata;
1015+
if ( type.isInstance( parameterMetadata ) ) {
1016+
return type.cast( parameterMetadata );
10181017
}
10191018

1020-
if ( cls.isInstance( paramBindings ) ) {
1021-
return (T) paramBindings;
1019+
if ( type.isInstance( parameterBindings ) ) {
1020+
return type.cast( parameterBindings );
10221021
}
10231022

1024-
if ( cls.isInstance( queryOptions ) ) {
1025-
return (T) queryOptions;
1023+
if ( type.isInstance( getQueryOptions() ) ) {
1024+
return type.cast( getQueryOptions() );
10261025
}
10271026

1028-
if ( cls.isInstance( getSession() ) ) {
1029-
return (T) getSession();
1027+
if ( type.isInstance( getQueryOptions().getAppliedGraph() ) ) {
1028+
return type.cast( getQueryOptions().getAppliedGraph() );
10301029
}
10311030

1032-
if ( ProcedureOutputs.class.isAssignableFrom( cls ) ) {
1033-
return (T) getOutputs();
1031+
if ( type.isInstance( getSession() ) ) {
1032+
return type.cast( getSession() );
10341033
}
10351034

1036-
throw new PersistenceException( "Unrecognized unwrap type : " + cls.getName() );
1035+
if ( type.isInstance( getOutputs() ) ) {
1036+
return type.cast( getOutputs() );
1037+
}
1038+
1039+
throw new PersistenceException( "Unrecognized unwrap type [" + type.getName() + "]" );
10371040
}
10381041

10391042
@Override

hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.hibernate.query.KeyedResultList;
5353
import org.hibernate.query.NativeQuery;
5454
import org.hibernate.query.Order;
55-
import org.hibernate.query.ParameterMetadata;
5655
import org.hibernate.query.PathException;
5756
import org.hibernate.query.Query;
5857
import org.hibernate.query.QueryParameter;
@@ -107,8 +106,6 @@
107106
import jakarta.persistence.AttributeConverter;
108107
import jakarta.persistence.CacheRetrieveMode;
109108
import jakarta.persistence.CacheStoreMode;
110-
import jakarta.persistence.EntityManager;
111-
import jakarta.persistence.EntityManagerFactory;
112109
import jakarta.persistence.FlushModeType;
113110
import jakarta.persistence.LockModeType;
114111
import jakarta.persistence.Parameter;
@@ -1334,29 +1331,32 @@ public NativeQueryImplementor<R> setReadOnly(boolean readOnly) {
13341331
}
13351332

13361333
@Override
1337-
@SuppressWarnings("unchecked")
1338-
public <T> T unwrap(Class<T> javaType) {
1339-
if ( javaType.isAssignableFrom( getClass() ) ) {
1340-
return (T) this;
1334+
public <T> T unwrap(Class<T> type) {
1335+
if ( type.isInstance( this ) ) {
1336+
return type.cast( this );
13411337
}
13421338

1343-
if ( javaType.isAssignableFrom( ParameterMetadata.class ) ) {
1344-
return (T) parameterMetadata;
1339+
if ( type.isInstance( parameterMetadata ) ) {
1340+
return type.cast( parameterMetadata );
13451341
}
13461342

1347-
if ( javaType.isAssignableFrom( QueryParameterBindings.class ) ) {
1348-
return (T) parameterBindings;
1343+
if ( type.isInstance( parameterBindings ) ) {
1344+
return type.cast( parameterBindings );
13491345
}
13501346

1351-
if ( javaType.isAssignableFrom( EntityManager.class ) ) {
1352-
return (T) getSession();
1347+
if ( type.isInstance( getQueryOptions() ) ) {
1348+
return type.cast( getQueryOptions() );
13531349
}
13541350

1355-
if ( javaType.isAssignableFrom( EntityManagerFactory.class ) ) {
1356-
return (T) getSession().getFactory();
1351+
if ( type.isInstance( getQueryOptions().getAppliedGraph() ) ) {
1352+
return type.cast( getQueryOptions().getAppliedGraph() );
13571353
}
13581354

1359-
throw new PersistenceException( "Unrecognized unwrap type [" + javaType.getName() + "]" );
1355+
if ( type.isInstance( getSession() ) ) {
1356+
return type.cast( getSession() );
1357+
}
1358+
1359+
throw new PersistenceException( "Unrecognized unwrap type [" + type.getName() + "]" );
13601360
}
13611361

13621362
@Override

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -931,33 +931,36 @@ public NamedSqmQueryMemento<R> toMemento(String name) {
931931
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
932932
// unwrap
933933

934-
@SuppressWarnings("unchecked")
935-
public <T> T unwrap(Class<T> cls) {
936-
if ( cls.isInstance( this ) ) {
937-
return (T) this;
934+
public <T> T unwrap(Class<T> type) {
935+
if ( type.isInstance( this ) ) {
936+
return type.cast( this );
938937
}
939938

940-
if ( cls.isInstance( parameterMetadata ) ) {
941-
return (T) parameterMetadata;
939+
if ( type.isInstance( parameterMetadata ) ) {
940+
return type.cast( parameterMetadata );
942941
}
943942

944-
if ( cls.isInstance( parameterBindings ) ) {
945-
return (T) parameterBindings;
943+
if ( type.isInstance( parameterBindings ) ) {
944+
return type.cast( parameterBindings );
946945
}
947946

948-
if ( cls.isInstance( sqm ) ) {
949-
return (T) sqm;
947+
if ( type.isInstance( sqm ) ) {
948+
return type.cast( sqm );
950949
}
951950

952-
if ( cls.isInstance( getQueryOptions() ) ) {
953-
return (T) getQueryOptions();
951+
if ( type.isInstance( getQueryOptions() ) ) {
952+
return type.cast( getQueryOptions() );
954953
}
955954

956-
if ( cls.isInstance( getQueryOptions().getAppliedGraph() ) ) {
957-
return (T) getQueryOptions().getAppliedGraph();
955+
if ( type.isInstance( getQueryOptions().getAppliedGraph() ) ) {
956+
return type.cast( getQueryOptions().getAppliedGraph() );
958957
}
959958

960-
throw new PersistenceException( "Unrecognized unwrap type [" + cls.getName() + "]" );
959+
if ( type.isInstance( getSession() ) ) {
960+
return type.cast( getSession() );
961+
}
962+
963+
throw new PersistenceException( "Unrecognized unwrap type [" + type.getName() + "]" );
961964
}
962965

963966

0 commit comments

Comments
 (0)