Skip to content

Commit f469677

Browse files
committed
HHH-18629 Fix inconsistent column alias generated while result class is used for placeholder
1 parent 3f6145b commit f469677

File tree

12 files changed

+227
-66
lines changed

12 files changed

+227
-66
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/NamedHqlQueryDefinitionImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public String getHqlString() {
7070
public NamedSqmQueryMemento resolve(SessionFactoryImplementor factory) {
7171
return new NamedHqlQueryMementoImpl(
7272
getRegistrationName(),
73+
null,
7374
hqlString,
7475
firstResult,
7576
maxResults,

hibernate-core/src/main/java/org/hibernate/boot/internal/NamedNativeQueryDefinitionImpl.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.hibernate.query.sql.internal.NamedNativeQueryMementoImpl;
1919
import org.hibernate.query.sql.spi.NamedNativeQueryMemento;
2020

21+
import org.checkerframework.checker.nullness.qual.Nullable;
22+
2123
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
2224

2325
/**
@@ -86,15 +88,16 @@ public String getResultSetMappingClassName() {
8688

8789
@Override
8890
public NamedNativeQueryMemento resolve(SessionFactoryImplementor factory) {
91+
Class<?> resultClass = isNotEmpty( resultSetMappingClassName )
92+
? factory.getServiceRegistry().requireService( ClassLoaderService.class ).classForName( resultSetMappingClassName )
93+
: null;
8994
return new NamedNativeQueryMementoImpl(
9095
getRegistrationName(),
96+
resultClass,
9197
sqlString,
9298
sqlString,
9399
resultSetMappingName,
94-
isNotEmpty( resultSetMappingClassName )
95-
? factory.getServiceRegistry().requireService( ClassLoaderService.class )
96-
.classForName( resultSetMappingClassName )
97-
: null,
100+
resultClass,
98101
querySpaces,
99102
getCacheable(),
100103
getCacheRegion(),

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

+23-20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.Function;
2020

2121
import jakarta.persistence.EntityGraph;
22+
import org.checkerframework.checker.nullness.qual.Nullable;
2223
import org.hibernate.CacheMode;
2324
import org.hibernate.EntityNameResolver;
2425
import org.hibernate.Filter;
@@ -891,22 +892,8 @@ public <T> QueryImplementor<T> createQuery(String queryString, Class<T> expected
891892
// dynamic native (SQL) query handling
892893

893894
@Override @SuppressWarnings("rawtypes")
894-
public NativeQueryImpl createNativeQuery(String sqlString) {
895-
checkOpen();
896-
pulseTransactionCoordinator();
897-
delayedAfterCompletion();
898-
899-
try {
900-
final NativeQueryImpl query = new NativeQueryImpl<>( sqlString, this );
901-
if ( isEmpty( query.getComment() ) ) {
902-
query.setComment( "dynamic native SQL query" );
903-
}
904-
applyQuerySettingsAndHints( query );
905-
return query;
906-
}
907-
catch (RuntimeException he) {
908-
throw getExceptionConverter().convert( he );
909-
}
895+
public NativeQueryImplementor createNativeQuery(String sqlString) {
896+
return createNativeQuery( sqlString, (Class) null );
910897
}
911898

912899
@Override @SuppressWarnings("rawtypes")
@@ -939,12 +926,28 @@ protected NamedResultSetMappingMemento getResultSetMappingMemento(String resultS
939926
@Override @SuppressWarnings({"rawtypes", "unchecked"})
940927
//note: we're doing something a bit funny here to work around
941928
// the clashing signatures declared by the supertypes
942-
public NativeQueryImplementor createNativeQuery(String sqlString, Class resultClass) {
943-
final NativeQueryImpl query = createNativeQuery( sqlString );
944-
addResultType( resultClass, query );
945-
return query;
929+
public NativeQueryImplementor createNativeQuery(String sqlString, @Nullable Class resultClass) {
930+
checkOpen();
931+
pulseTransactionCoordinator();
932+
delayedAfterCompletion();
933+
934+
try {
935+
final NativeQueryImpl query = new NativeQueryImpl<>( sqlString, resultClass, this );
936+
if ( isEmpty( query.getComment() ) ) {
937+
query.setComment( "dynamic native SQL query" );
938+
}
939+
applyQuerySettingsAndHints( query );
940+
return query;
941+
}
942+
catch (RuntimeException he) {
943+
throw getExceptionConverter().convert( he );
944+
}
946945
}
947946

947+
/**
948+
* @deprecated Use {@link NativeQueryImpl#NativeQueryImpl(String, Class, SharedSessionContractImplementor)} instead
949+
*/
950+
@Deprecated(forRemoval = true)
948951
protected <T> void addResultType(Class<T> resultClass, NativeQueryImplementor<T> query) {
949952
if ( Tuple.class.equals( resultClass ) ) {
950953
query.setTupleTransformer( NativeQueryTupleTransformer.INSTANCE );

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

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public NamedCallableQueryMementoImpl(
6464
Map<String, Object> hints) {
6565
super(
6666
name,
67+
Object.class,
6768
cacheable,
6869
cacheRegion,
6970
cacheMode,

hibernate-core/src/main/java/org/hibernate/query/criteria/internal/NamedCriteriaQueryMementoImpl.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.hibernate.query.sqm.spi.NamedSqmQueryMemento;
2323
import org.hibernate.query.sqm.tree.SqmStatement;
2424

25+
import org.checkerframework.checker.nullness.qual.Nullable;
26+
2527
public class NamedCriteriaQueryMementoImpl extends AbstractNamedQueryMemento implements NamedSqmQueryMemento, Serializable {
2628

2729
private final SqmStatement sqmStatement;
@@ -33,6 +35,7 @@ public class NamedCriteriaQueryMementoImpl extends AbstractNamedQueryMemento imp
3335

3436
public NamedCriteriaQueryMementoImpl(
3537
String name,
38+
@Nullable Class<?> resultType,
3639
SqmStatement sqmStatement,
3740
Integer firstResult,
3841
Integer maxResults,
@@ -47,7 +50,7 @@ public NamedCriteriaQueryMementoImpl(
4750
String comment,
4851
Map<String, String> parameterTypes,
4952
Map<String, Object> hints) {
50-
super( name, cacheable, cacheRegion, cacheMode, flushMode, readOnly, timeout, fetchSize, comment, hints );
53+
super( name, resultType, cacheable, cacheRegion, cacheMode, flushMode, readOnly, timeout, fetchSize, comment, hints );
5154
this.sqmStatement = sqmStatement;
5255
this.firstResult = firstResult;
5356
this.maxResults = maxResults;
@@ -114,6 +117,7 @@ public Map<String, String> getParameterTypes() {
114117
public NamedSqmQueryMemento makeCopy(String name) {
115118
return new NamedCriteriaQueryMementoImpl(
116119
name,
120+
getResultType(),
117121
sqmStatement,
118122
firstResult,
119123
maxResults,

hibernate-core/src/main/java/org/hibernate/query/hql/internal/NamedHqlQueryMementoImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.hibernate.query.sqm.spi.NamedSqmQueryMemento;
2323
import org.hibernate.query.sqm.tree.SqmStatement;
2424

25+
import org.checkerframework.checker.nullness.qual.Nullable;
26+
2527
/**
2628
* Definition of a named query, defined in the mapping metadata.
2729
* Additionally, as of JPA 2.1, named query definition can also come
@@ -41,6 +43,7 @@ public class NamedHqlQueryMementoImpl extends AbstractNamedQueryMemento implemen
4143

4244
public NamedHqlQueryMementoImpl(
4345
String name,
46+
@Nullable Class<?> resultType,
4447
String hqlString,
4548
Integer firstResult,
4649
Integer maxResults,
@@ -57,6 +60,7 @@ public NamedHqlQueryMementoImpl(
5760
Map<String,Object> hints) {
5861
super(
5962
name,
63+
resultType,
6064
cacheable,
6165
cacheRegion,
6266
cacheMode,
@@ -103,6 +107,7 @@ public Map<String, String> getParameterTypes() {
103107
public NamedSqmQueryMemento makeCopy(String name) {
104108
return new NamedHqlQueryMementoImpl(
105109
name,
110+
getResultType(),
106111
hqlString,
107112
firstResult,
108113
maxResults,

hibernate-core/src/main/java/org/hibernate/query/named/AbstractNamedQueryMemento.java

+9
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
import org.hibernate.CacheMode;
1515
import org.hibernate.FlushMode;
1616

17+
import org.checkerframework.checker.nullness.qual.Nullable;
18+
1719
/**
1820
* @author Steve Ebersole
1921
* @author Gavin King
2022
*/
2123
public abstract class AbstractNamedQueryMemento implements NamedQueryMemento {
2224
private final String name;
25+
private final @Nullable Class<?> resultType;
2326

2427
private final Boolean cacheable;
2528
private final String cacheRegion;
@@ -37,6 +40,7 @@ public abstract class AbstractNamedQueryMemento implements NamedQueryMemento {
3740

3841
protected AbstractNamedQueryMemento(
3942
String name,
43+
@Nullable Class<?> resultType,
4044
Boolean cacheable,
4145
String cacheRegion,
4246
CacheMode cacheMode,
@@ -47,6 +51,7 @@ protected AbstractNamedQueryMemento(
4751
String comment,
4852
Map<String, Object> hints) {
4953
this.name = name;
54+
this.resultType = resultType;
5055
this.cacheable = cacheable;
5156
this.cacheRegion = cacheRegion;
5257
this.cacheMode = cacheMode;
@@ -63,6 +68,10 @@ public String getRegistrationName() {
6368
return name;
6469
}
6570

71+
public @Nullable Class<?> getResultType() {
72+
return resultType;
73+
}
74+
6675
@Override
6776
public Boolean getCacheable() {
6877
return cacheable;

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class NamedNativeQueryMementoImpl extends AbstractNamedQueryMemento imple
3838

3939
public NamedNativeQueryMementoImpl(
4040
String name,
41+
Class<?> resultClass,
4142
String sqlString,
4243
String originalSqlString,
4344
String resultSetMappingName,
@@ -56,6 +57,7 @@ public NamedNativeQueryMementoImpl(
5657
Map<String,Object> hints) {
5758
super(
5859
name,
60+
resultClass,
5961
cacheable,
6062
cacheRegion,
6163
cacheMode,
@@ -123,6 +125,7 @@ public Integer getMaxResults() {
123125
public NamedNativeQueryMemento makeCopy(String name) {
124126
return new NamedNativeQueryMementoImpl(
125127
name,
128+
getResultType(),
126129
sqlString,
127130
originalSqlString,
128131
resultSetMappingName,
@@ -149,7 +152,8 @@ public void validate(QueryEngine queryEngine) {
149152

150153
@Override
151154
public <T> NativeQueryImplementor<T> toQuery(SharedSessionContractImplementor session) {
152-
return new NativeQueryImpl<>( this, session );
155+
//noinspection unchecked
156+
return new NativeQueryImpl<>( this, (Class<T>) getResultType(), session );
153157
}
154158

155159
@Override

0 commit comments

Comments
 (0)