{"record":{"id":"aa6ec5e5aef59cef","repo":"hibernate/hibernate-orm","slug":"full-join-emulation-requires-order-by-expressions","errorCode":null,"errorMessage":"Full join emulation requires order by expressions to be in the select list for grouped or distinct queries","messagePattern":"Full join emulation requires order by expressions to be in the select list for grouped or distinct queries","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/ast/spi/FullJoinEmulation.java","lineNumber":172,"sourceCode":"\t\treturn fullJoinEmulationBranches != null\n\t\t\t&& ( queryPart == fullJoinEmulationQueryGroup\n\t\t\t\t// Some dialects (e.g. Sybase ASE) render ORDER BY outside the query group scope.\n\t\t\t\t|| fullJoinEmulationQueryGroup != null\n\t\t\t\t\t&& sortSpecifications == fullJoinEmulationQueryGroup.getSortSpecifications() );\n\t}\n\n\tprivate void emulateFullJoinWithUnion(QuerySpec querySpec, List<FullJoinEmulationInfo> fullJoins) {\n\t\tfinal var sortSpecifications = querySpec.getSortSpecifications();\n\t\tfinal List<SortSpecification> preservedSortSpecifications =\n\t\t\t\tsortSpecifications == null ? null : new ArrayList<>( sortSpecifications );\n\t\tfinal var extraSelections =\n\t\t\t\tcollectFullJoinEmulationExtraSelections( querySpec,\n\t\t\t\t\t\tcountRenderedSelectItems( querySpec.getSelectClause() ) );\n\t\tif ( !extraSelections.isEmpty() ) {\n\t\t\tif ( querySpec.getSelectClause().isDistinct() // TODO: we could easily remove this limitation\n\t\t\t\t\t|| !querySpec.getGroupByClauseExpressions().isEmpty()\n\t\t\t\t\t|| querySpec.getHavingClauseRestrictions() != null ) {\n\t\t\t\tthrow new UnsupportedOperationException(\n\t\t\t\t\t\t\"Full join emulation requires order by expressions to be in the select list for grouped or distinct queries\"\n\t\t\t\t);\n\t\t\t}\n\t\t\tfullJoinEmulationExtraSelections = extraSelections;\n\t\t}\n\n\t\tfinal var emulationPlan = createFullJoinEmulationPlan( querySpec, fullJoins );\n\t\tfinal var queryGroup = emulationPlan.queryGroup();\n\t\tfinal var branches = emulationPlan.branches();\n\n\t\tcopyOrderAndOffsetFetch( querySpec, preservedSortSpecifications, queryGroup );\n\n\t\tfullJoinEmulationInfos = fullJoins;\n\t\tfullJoinEmulationBranches = branches;\n\t\tfullJoinEmulationQueryGroup = queryGroup;\n\t\ttry {\n\t\t\tqueryGroup.accept( translator );\n\t\t}","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/FullJoinEmulation.java#L154-L190","documentation":"Error \"Full join emulation requires order by expressions to be in the select list for grouped or distinct queries\" thrown in hibernate/hibernate-orm.","triggerScenarios":"A full outer join is emulated (via union) on a grouped or distinct query whose ORDER BY expressions are not in the select list.","commonSituations":"Ordering by a non-selected column with full join on a dialect without native FULL JOIN; add the expression to the select list.","solutions":["Add every ORDER BY expression to the select list of the grouped/distinct query so full join emulation can sort on it.","Remove ORDER BY items that are not in the select list, or drop DISTINCT/GROUP BY if they are not required.","If the database supports FULL JOIN natively, avoid the emulation path by using a dialect/version that does."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}