@@ -119,6 +119,10 @@ public SqmSelectStatement<T> copy(SqmCopyContext context) {
119119 if ( existing != null ) {
120120 return existing ;
121121 }
122+ return createCopy ( context , getResultType () );
123+ }
124+
125+ private <X > SqmSelectStatement <X > createCopy (SqmCopyContext context , Class <X > resultType ) {
122126 final Set <SqmParameter <?>> parameters ;
123127 if ( this .parameters == null ) {
124128 parameters = null ;
@@ -129,17 +133,19 @@ public SqmSelectStatement<T> copy(SqmCopyContext context) {
129133 parameters .add ( parameter .copy ( context ) );
130134 }
131135 }
132- final SqmSelectStatement <T > statement = context .registerCopy (
136+ //noinspection unchecked
137+ final SqmSelectStatement <X > statement = (SqmSelectStatement <X >) context .registerCopy (
133138 this ,
134139 new SqmSelectStatement <>(
135140 nodeBuilder (),
136141 copyCteStatements ( context ),
137- getResultType () ,
142+ resultType ,
138143 getQuerySource (),
139144 parameters
140145 )
141146 );
142- statement .setQueryPart ( getQueryPart ().copy ( context ) );
147+ //noinspection unchecked
148+ statement .setQueryPart ( (SqmQueryPart <X >) getQueryPart ().copy ( context ) );
143149 return statement ;
144150 }
145151
@@ -266,9 +272,6 @@ public SqmSelectStatement<T> select(Selection<? extends T> selection) {
266272 checkSelectionIsJpaCompliant ( selection );
267273 }
268274 getQuerySpec ().setSelection ( (JpaSelection <T >) selection );
269- if ( getResultType () == Object .class ) {
270- setResultType ( (Class <T >) selection .getJavaType () );
271- }
272275 return this ;
273276 }
274277
@@ -313,7 +316,6 @@ private Selection<? extends T> getResultSelection(List<?> selectionList) {
313316 return (Selection <? extends T >) selectionList .get ( 0 );
314317 }
315318 default : {
316- setResultType ( (Class <T >) Object [].class );
317319 return (Selection <? extends T >) nodeBuilder ().array ( selections );
318320 }
319321 }
@@ -461,14 +463,14 @@ private void validateComplianceFetchOffset() {
461463
462464 @ Override
463465 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 ;
467469 //TODO: detect queries with no 'group by', but aggregate functions
468470 // in 'select' list (we don't even need to hit the database to
469471 // know they return exactly one row)
470472 if ( queryPart .isSimpleQueryPart ()
471- && !querySpec .isDistinct ()
473+ && !( querySpec = ( SqmQuerySpec <?>) queryPart ) .isDistinct ()
472474 && querySpec .getGroupingExpressions ().isEmpty () ) {
473475 for ( SqmRoot <?> root : querySpec .getRootList () ) {
474476 root .removeLeftFetchJoins ();
@@ -478,24 +480,21 @@ public SqmSelectStatement<Long> createCountQuery() {
478480 querySpec .setOrderByClause ( null );
479481 }
480482
481- @ SuppressWarnings ("unchecked" )
482- final SqmSelectStatement <Long > statement = (SqmSelectStatement <Long >) copy ;
483- statement .setResultType ( Long .class );
484- return statement ;
483+ return copy ;
485484 }
486485 else {
487- final JpaSelection <?> selection = querySpec .getSelection ();
486+ final JpaSelection <?> selection = queryPart . getFirstQuerySpec () .getSelection ();
488487 if ( selection .isCompoundSelection () ) {
489488 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 ) + '_' );
492491 }
493492 }
494493 else {
495- selection .alias ("a_" );
494+ selection .alias ( "a_" );
496495 }
497496 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 );
499498 query .from ( subquery );
500499 query .select ( nodeBuilder ().count () );
501500 return query ;
0 commit comments