{"record":{"id":"94f2ebe306f4f009","repo":"hibernate/hibernate-orm","slug":"sortspecification-selection-indexes-size-mismatch","errorCode":null,"errorMessage":"SortSpecification selection indexes size mismatch","messagePattern":"SortSpecification selection indexes size mismatch","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/ast/spi/FullJoinEmulation.java","lineNumber":675,"sourceCode":"\t\tfinal var expressions = getSortExpressions( sortSpecification );\n\t\tfinal int[] indexes = new int[expressions.size()];\n\t\tfor ( int i = 0; i < expressions.size(); i++ ) {\n\t\t\textraSelections.add( createSortNullPrecedenceEmulationExpression( expressions.get( i ), nullPrecedence ) );\n\t\t\tindexes[i] = selectItemCount + extraSelections.size() - 1;\n\t\t}\n\t\tnullPrecedenceSelectionIndexes.put( sortSpecification, indexes );\n\t}\n\n\tprivate List<? extends Expression> getSortExpressions(SortSpecification sortSpecification) {\n\t\tfinal var sortExpression = sortSpecification.getSortExpression();\n\t\tfinal int[] sortSelectionIndexes = sortSpecification.getSortSelectionIndexes();\n\t\tif ( sortSelectionIndexes == null ) {\n\t\t\tthrow new IllegalStateException( \"SortSpecification is missing selection indexes\" );\n\t\t}\n\t\tfinal var tuple = getSqlTuple( sortExpression );\n\t\tfinal var expressions = tuple == null ? List.of( sortExpression ) : tuple.getExpressions();\n\t\tif ( sortSelectionIndexes.length != expressions.size() ) {\n\t\t\tthrow new IllegalStateException( \"SortSpecification selection indexes size mismatch\" );\n\t\t}\n\t\treturn expressions;\n\t}\n\n\tprivate Nulls resolveRenderedNullPrecedence(SortSpecification sortSpecification) {\n\t\tfinal var sortOrder = sortSpecification.getSortOrder();\n\t\tNulls nullPrecedence = sortSpecification.getNullPrecedence();\n\t\tif ( nullPrecedence == null || nullPrecedence == Nulls.NONE ) {\n\t\t\tnullPrecedence =\n\t\t\t\t\ttranslator.getSessionFactory().getSessionFactoryOptions()\n\t\t\t\t\t\t\t.getDefaultNullPrecedence();\n\t\t}\n\t\tif ( isDefaultOrdering( nullPrecedence, sortOrder,\n\t\t\t\ttranslator.getDialect().getNullOrdering() ) ) {\n\t\t\treturn null;\n\t\t}\n\t\treturn nullPrecedence;\n\t}","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/FullJoinEmulation.java#L657-L693","documentation":"Error \"SortSpecification selection indexes size mismatch\" thrown in hibernate/hibernate-orm.","triggerScenarios":"The number of selection indexes in the SortSpecification does not match the expected sort specification size during full-join emulation.","commonSituations":"Full outer join with ORDER BY on a dialect that emulates full joins via UNION, when sort items cannot be aligned with the emulated select list.","solutions":["This is an internal Hibernate consistency check during full-join emulation: the number of selection indexes does not match the expected size. Report it as a Hibernate bug with the stack trace and the originating query.","As a workaround, restructure the query to avoid full-join emulation (e.g. rewrite the join type or split the query)."],"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"}