@@ -318,18 +318,33 @@ private void renderFromClause(StringBuilder jpaqlQuery, RenderingContext renderi
318
318
final FromImplementor correlationParent = correlationRoot .getCorrelationParent ();
319
319
correlationParent .prepareAlias ( renderingContext );
320
320
final String correlationRootAlias = correlationParent .getAlias ();
321
- for ( Join <?, ?> correlationJoin : correlationRoot .getJoins () ) {
322
- final JoinImplementor correlationJoinImpl = (JoinImplementor ) correlationJoin ;
323
- // IMPL NOTE: reuse the sep from above!
321
+ if ( correlationRoot .canBeReplacedByCorrelatedParentInSubQuery () ) {
322
+ for ( Join <?, ?> correlationJoin : correlationRoot .getJoins () ) {
323
+ final JoinImplementor correlationJoinImpl = (JoinImplementor ) correlationJoin ;
324
+ // IMPL NOTE: reuse the sep from above!
325
+ jpaqlQuery .append ( sep );
326
+ correlationJoinImpl .prepareAlias ( renderingContext );
327
+ jpaqlQuery .append ( correlationRootAlias )
328
+ .append ( '.' )
329
+ .append ( correlationJoinImpl .getAttribute ().getName () )
330
+ .append ( " as " )
331
+ .append ( correlationJoinImpl .getAlias () );
332
+ sep = ", " ;
333
+ renderJoins ( jpaqlQuery , renderingContext , correlationJoinImpl .getJoins () );
334
+ }
335
+ }
336
+ else {
337
+ correlationRoot .prepareAlias ( renderingContext );
324
338
jpaqlQuery .append ( sep );
325
- correlationJoinImpl .prepareAlias ( renderingContext );
326
- jpaqlQuery .append ( correlationRootAlias )
327
- .append ( '.' )
328
- .append ( correlationJoinImpl .getAttribute ().getName () )
329
- .append ( " as " )
330
- .append ( correlationJoinImpl .getAlias () );
331
339
sep = ", " ;
332
- renderJoins ( jpaqlQuery , renderingContext , correlationJoinImpl .getJoins () );
340
+ jpaqlQuery .append ( correlationRoot .renderTableExpression ( renderingContext ) );
341
+ renderJoins ( jpaqlQuery , renderingContext , correlationRoot .getJoins () );
342
+ if ( correlationRoot instanceof Root ) {
343
+ Set <TreatedRoot > treats = ( (RootImpl ) correlationRoot ).getTreats ();
344
+ for ( TreatedRoot treat : treats ) {
345
+ renderJoins ( jpaqlQuery , renderingContext , treat .getJoins () );
346
+ }
347
+ }
333
348
}
334
349
}
335
350
}
@@ -341,20 +356,48 @@ private void renderFromClause(StringBuilder jpaqlQuery, RenderingContext renderi
341
356
}
342
357
343
358
protected void renderWhereClause (StringBuilder jpaqlQuery , RenderingContext renderingContext ) {
344
- if ( getRestriction () == null ) {
359
+ final String correlationRestrictionWhereFragment = getCorrelationRestrictionsWhereFragment ();
360
+ if ( getRestriction () == null && correlationRestrictionWhereFragment .isEmpty () ) {
345
361
return ;
346
362
}
347
363
348
364
renderingContext .getClauseStack ().push ( Clause .WHERE );
349
365
try {
350
- jpaqlQuery .append ( " where " )
351
- .append ( ( (Renderable ) getRestriction () ).render ( renderingContext ) );
366
+ jpaqlQuery .append ( " where " );
367
+ jpaqlQuery .append ( correlationRestrictionWhereFragment );
368
+ if ( getRestriction () != null ) {
369
+ if ( !correlationRestrictionWhereFragment .isEmpty () ) {
370
+ jpaqlQuery .append ( " and ( " );
371
+ }
372
+ jpaqlQuery .append ( ( (Renderable ) getRestriction () ).render ( renderingContext ) );
373
+ if ( !correlationRestrictionWhereFragment .isEmpty () ) {
374
+ jpaqlQuery .append ( " )" );
375
+ }
376
+ }
352
377
}
353
378
finally {
354
379
renderingContext .getClauseStack ().pop ();
355
380
}
356
381
}
357
382
383
+ private String getCorrelationRestrictionsWhereFragment () {
384
+ if ( !isSubQuery || correlationRoots == null ) {
385
+ return "" ;
386
+ }
387
+ StringBuilder buffer = new StringBuilder ();
388
+ String sep = "" ;
389
+ for ( FromImplementor <?, ?> correlationRoot : correlationRoots ) {
390
+ if ( !correlationRoot .canBeReplacedByCorrelatedParentInSubQuery () ) {
391
+ buffer .append ( sep );
392
+ sep = " and " ;
393
+ buffer .append ( correlationRoot .getAlias () )
394
+ .append ( "=" )
395
+ .append ( correlationRoot .getCorrelationParent ().getAlias () );
396
+ }
397
+ }
398
+ return buffer .toString ();
399
+ }
400
+
358
401
protected void renderGroupByClause (StringBuilder jpaqlQuery , RenderingContext renderingContext ) {
359
402
if ( getGroupings ().isEmpty () ) {
360
403
return ;
@@ -395,7 +438,7 @@ private void renderHavingClause(StringBuilder jpaqlQuery, RenderingContext rende
395
438
private void renderJoins (
396
439
StringBuilder jpaqlQuery ,
397
440
RenderingContext renderingContext ,
398
- Collection <Join <?,?>> joins ) {
441
+ Collection <? extends Join <?,?>> joins ) {
399
442
if ( joins == null ) {
400
443
return ;
401
444
}
@@ -428,7 +471,7 @@ private String renderJoinType(JoinType joinType) {
428
471
private void renderFetches (
429
472
StringBuilder jpaqlQuery ,
430
473
RenderingContext renderingContext ,
431
- Collection <Fetch > fetches ) {
474
+ Collection <? extends Fetch > fetches ) {
432
475
if ( fetches == null ) {
433
476
return ;
434
477
}
0 commit comments