@@ -119,6 +119,10 @@ public SqmSelectStatement<T> copy(SqmCopyContext context) {
119
119
if ( existing != null ) {
120
120
return existing ;
121
121
}
122
+ return createCopy ( context , getResultType () );
123
+ }
124
+
125
+ private <X > SqmSelectStatement <X > createCopy (SqmCopyContext context , Class <X > resultType ) {
122
126
final Set <SqmParameter <?>> parameters ;
123
127
if ( this .parameters == null ) {
124
128
parameters = null ;
@@ -129,17 +133,19 @@ public SqmSelectStatement<T> copy(SqmCopyContext context) {
129
133
parameters .add ( parameter .copy ( context ) );
130
134
}
131
135
}
132
- final SqmSelectStatement <T > statement = context .registerCopy (
136
+ //noinspection unchecked
137
+ final SqmSelectStatement <X > statement = (SqmSelectStatement <X >) context .registerCopy (
133
138
this ,
134
139
new SqmSelectStatement <>(
135
140
nodeBuilder (),
136
141
copyCteStatements ( context ),
137
- getResultType () ,
142
+ resultType ,
138
143
getQuerySource (),
139
144
parameters
140
145
)
141
146
);
142
- statement .setQueryPart ( getQueryPart ().copy ( context ) );
147
+ //noinspection unchecked
148
+ statement .setQueryPart ( (SqmQueryPart <X >) getQueryPart ().copy ( context ) );
143
149
return statement ;
144
150
}
145
151
@@ -266,9 +272,6 @@ public SqmSelectStatement<T> select(Selection<? extends T> selection) {
266
272
checkSelectionIsJpaCompliant ( selection );
267
273
}
268
274
getQuerySpec ().setSelection ( (JpaSelection <T >) selection );
269
- if ( getResultType () == Object .class ) {
270
- setResultType ( (Class <T >) selection .getJavaType () );
271
- }
272
275
return this ;
273
276
}
274
277
@@ -313,7 +316,6 @@ private Selection<? extends T> getResultSelection(List<?> selectionList) {
313
316
return (Selection <? extends T >) selectionList .get ( 0 );
314
317
}
315
318
default : {
316
- setResultType ( (Class <T >) Object [].class );
317
319
return (Selection <? extends T >) nodeBuilder ().array ( selections );
318
320
}
319
321
}
@@ -461,14 +463,14 @@ private void validateComplianceFetchOffset() {
461
463
462
464
@ Override
463
465
public SqmSelectStatement <Long > createCountQuery () {
464
- final SqmSelectStatement <? > copy = copy ( noParamCopyContext () );
465
- final SqmQuerySpec <?> querySpec = copy . getQuerySpec ();
466
- final SqmQueryPart <?> queryPart = copy . getQueryPart () ;
466
+ final SqmSelectStatement <Long > copy = createCopy ( noParamCopyContext (), Long . class );
467
+ final SqmQueryPart <?> queryPart = getQueryPart ();
468
+ final SqmQuerySpec <?> querySpec ;
467
469
//TODO: detect queries with no 'group by', but aggregate functions
468
470
// in 'select' list (we don't even need to hit the database to
469
471
// know they return exactly one row)
470
472
if ( queryPart .isSimpleQueryPart ()
471
- && !querySpec .isDistinct ()
473
+ && !( querySpec = ( SqmQuerySpec <?>) queryPart ) .isDistinct ()
472
474
&& querySpec .getGroupingExpressions ().isEmpty () ) {
473
475
for ( SqmRoot <?> root : querySpec .getRootList () ) {
474
476
root .removeLeftFetchJoins ();
@@ -478,24 +480,21 @@ public SqmSelectStatement<Long> createCountQuery() {
478
480
querySpec .setOrderByClause ( null );
479
481
}
480
482
481
- @ SuppressWarnings ("unchecked" )
482
- final SqmSelectStatement <Long > statement = (SqmSelectStatement <Long >) copy ;
483
- statement .setResultType ( Long .class );
484
- return statement ;
483
+ return copy ;
485
484
}
486
485
else {
487
- final JpaSelection <?> selection = querySpec .getSelection ();
486
+ final JpaSelection <?> selection = queryPart . getFirstQuerySpec () .getSelection ();
488
487
if ( selection .isCompoundSelection () ) {
489
488
char c = 'a' ;
490
- for ( JpaSelection <?> item : selection .getSelectionItems () ) {
491
- item .alias ( Character .toString (++c ) + '_' );
489
+ for ( JpaSelection <?> item : selection .getSelectionItems () ) {
490
+ item .alias ( Character .toString ( ++c ) + '_' );
492
491
}
493
492
}
494
493
else {
495
- selection .alias ("a_" );
494
+ selection .alias ( "a_" );
496
495
}
497
496
final SqmSubQuery <?> subquery = new SqmSubQuery <>( copy , queryPart , null , nodeBuilder () );
498
- final SqmSelectStatement <Long > query = nodeBuilder ().createQuery (Long .class );
497
+ final SqmSelectStatement <Long > query = nodeBuilder ().createQuery ( Long .class );
499
498
query .from ( subquery );
500
499
query .select ( nodeBuilder ().count () );
501
500
return query ;
0 commit comments