@@ -362,7 +362,8 @@ private void buildExtraJoins(STreeType desc, List<SqlTreeNode> myList) {
362362 // the 'select' part of the query. We may need to add other joins to
363363 // support the predicates or order by clauses.
364364
365- // remove ManyWhereJoins from the predicateIncludes
365+ // remove ManyWhereJoins from the predicateIncludes and orderByIncludes
366+ predicates .orderByIncludes ().removeAll (manyWhereJoins .propertyNames ());
366367 predicateIncludes .removeAll (manyWhereJoins .propertyNames ());
367368 predicateIncludes .addAll (predicates .orderByIncludes ());
368369
@@ -649,11 +650,15 @@ private void createExtraJoin(String includeProp) {
649650 * Create a SqlTreeNodeExtraJoin, register and return it.
650651 */
651652 private SqlTreeNodeExtraJoin createJoinLeaf (String propertyName ) {
653+ SqlTreeNodeExtraJoin extraJoin = joinRegister .get (propertyName );
654+ if (extraJoin != null ) {
655+ return extraJoin ;
656+ }
652657 ExtraJoin extra = desc .extraJoin (propertyName );
653658 if (extra == null ) {
654659 return null ;
655660 } else {
656- SqlTreeNodeExtraJoin extraJoin = new SqlTreeNodeExtraJoin (propertyName , extra .property (), extra .isContainsMany (), temporalMode );
661+ extraJoin = new SqlTreeNodeExtraJoin (propertyName , extra .property (), extra .isContainsMany (), temporalMode );
657662 joinRegister .put (propertyName , extraJoin );
658663 return extraJoin ;
659664 }
@@ -674,24 +679,25 @@ private SqlTreeNodeExtraJoin findExtraJoinRoot(String includeProp, SqlTreeNodeEx
674679 // no parent possible(parent is root)
675680 return childJoin ;
676681
677- } else {
678- // look in register ...
679- String parentPropertyName = includeProp .substring (0 , dotPos );
680- if (selectIncludes .contains (parentPropertyName )) {
681- // parent already handled by select
682- return childJoin ;
683- }
684-
685- SqlTreeNodeExtraJoin parentJoin = joinRegister .get (parentPropertyName );
686- if (parentJoin == null ) {
687- // we need to create this the parent implicitly...
688- parentJoin = createJoinLeaf (parentPropertyName );
689- }
690-
691- parentJoin .addChild (childJoin );
692- childJoin = parentJoin ;
682+ }
683+ String parentPropertyName = includeProp .substring (0 , dotPos );
684+ if (desc .isEmbeddedPath (parentPropertyName )) {
685+ // digging in embedded property
686+ // so we have to join parent property path if any
693687 includeProp = parentPropertyName ;
688+ continue ;
689+ }
690+ // look in register ...
691+ if (selectIncludes .contains (parentPropertyName )) {
692+ // parent already handled by select
693+ return childJoin ;
694694 }
695+
696+ SqlTreeNodeExtraJoin parentJoin = createJoinLeaf (parentPropertyName );
697+
698+ parentJoin .addChild (childJoin );
699+ childJoin = parentJoin ;
700+ includeProp = parentPropertyName ;
695701 }
696702 }
697703
0 commit comments