{"record":{"id":"999f0e6e38b37ebd","repo":"hibernate/hibernate-orm","slug":"cannot-unwrap-to-requested-type-unwraptype-ge-999f0e","errorCode":null,"errorMessage":"Cannot unwrap to requested type [\" + unwrapType.getName() + \"]","messagePattern":"Cannot unwrap to requested type \\[\" \\+ unwrapType\\.getName\\(\\) \\+ \"\\]","errorType":"exception","errorClass":"UnknownUnwrapTypeException","httpStatus":null,"severity":"error","filePath":"hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java","lineNumber":145,"sourceCode":"\t}\n\n\t@Override\n\tpublic boolean isUnwrappableAs(@Nonnull Class<?> unwrapType) {\n\t\treturn unwrapType.isAssignableFrom( C3P0ConnectionProvider.class )\n\t\t\t|| unwrapType.isAssignableFrom( DataSource.class );\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\tpublic <T> T unwrap(@Nonnull Class<T> unwrapType) {\n\t\tif ( unwrapType.isAssignableFrom( C3P0ConnectionProvider.class ) ) {\n\t\t\treturn (T) this;\n\t\t}\n\t\telse if ( unwrapType.isAssignableFrom( DataSource.class ) ) {\n\t\t\treturn (T) dataSource;\n\t\t}\n\t\telse {\n\t\t\tthrow new UnknownUnwrapTypeException( unwrapType );\n\t\t}\n\t}\n\n\t@Override\n\tpublic void configure(@Nonnull Map<String, Object> properties) {\n\t\tCONNECTION_INFO_LOGGER.configureConnectionPool( \"c3p0\" );\n\n\t\tfinal String jdbcDriverClass = extractSetting(\n\t\t\t\tproperties,\n\t\t\t\tJdbcSettings.JAKARTA_JDBC_DRIVER,\n\t\t\t\tJdbcSettings.DRIVER,\n\t\t\t\tJdbcSettings.JPA_JDBC_DRIVER\n\t\t);\n\t\tfinal String jdbcUrl = extractSetting(\n\t\t\t\tproperties,\n\t\t\t\tJdbcSettings.JAKARTA_JDBC_URL,\n\t\t\t\tJdbcSettings.URL,\n\t\t\t\tJdbcSettings.JPA_JDBC_URL","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java#L127-L163","documentation":"The full-join emulation rewrites each ORDER BY item of the union branches as a positional reference (ordinal N) into the select list, so every sort expression must correspond to a select item whose index was recorded during SQM-to-SQL translation. This UnsupportedOperationException means the sort expression carried no matching select item (sortSelectionIndex == -1), typically because the ordered expression is computed only in the ORDER BY clause and was never selected.","triggerScenarios":"A full join query on MySQL/MariaDB/Sybase/ASE/H2/TiDB with an ORDER BY expression that does not appear in the select list - e.g. 'select a.id from A a full join b on ... order by b.sortKey' where b.sortKey is not selected. Note this variant fires even without distinct/group-by; with distinct/group-by the sibling check at line 172 fires first.","commonSituations":"Sorting by a column of the full-joined side while selecting only fields of the driving entity; pagination requests that add dynamic sort columns not present in the projection; queries ported from dialects where sorting by non-selected columns is routine.","solutions":["Add the sort expression to the select list so the emulation can reference it by ordinal.","Wrap the full join in a subquery and sort in the outer query, where plain expression ordering works.","Select and then ignore the extra column in your result mapping (Object[] or projection DTO).","Replace the full join with a hand-written union of left/right joins if the projection must stay narrow."],"exampleFix":"// before: order by expression not selected -> error on MySQL\nList<A> list = session.createQuery(\n    \"select a from A a full join a.items i order by i.label\", A.class)\n    .getResultList();\n\n// after: select the sort expression too\nList<Object[]> list = session.createQuery(\n    \"select a, i.label from A a full join a.items i order by i.label\", Object[].class)\n    .getResultList();","handlingStrategy":"try-catch","validationCode":"// For string queries: every order-by expression must appear in the select clause\nstatic boolean orderByCovered(String selectClause, List<String> orderByExprs) {\n    return orderByExprs.stream().allMatch( selectClause::contains );\n}","typeGuard":null,"tryCatchPattern":"try {\n    return em.createQuery(hql, type).getResultList();\n} catch ( UnsupportedOperationException e ) {\n    if ( e.getMessage() != null && e.getMessage().contains(\"order by expressions to be in the select list for union queries\") ) {\n        return em.createQuery(addSortKeysToSelect(hql), Object[].class).getResultList();\n    }\n    throw e;\n}","preventionTips":["Treat 'order-by keys must be selected' as a coding convention for full-join queries on emulating dialects.","For dynamic sorting grids, maintain the projection in lockstep with the requested sort columns.","Validate generated HQL at build time (parse with SessionFactory#hqlQuery at startup) to catch shape errors early."],"tags":["full-join","order-by","select-list","dialect-limitation","union"],"backgroundTag":"full-join-emulation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}