hibernate/hibernate-orm · error · UnknownUnwrapTypeException

Cannot unwrap to requested type [" + unwrapType.getName() +

Error message

Cannot unwrap to requested type [" + unwrapType.getName() + "]

What it means

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.

Source

Thrown at hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java:145

	}

	@Override
	public boolean isUnwrappableAs(@Nonnull Class<?> unwrapType) {
		return unwrapType.isAssignableFrom( C3P0ConnectionProvider.class )
			|| unwrapType.isAssignableFrom( DataSource.class );
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T unwrap(@Nonnull Class<T> unwrapType) {
		if ( unwrapType.isAssignableFrom( C3P0ConnectionProvider.class ) ) {
			return (T) this;
		}
		else if ( unwrapType.isAssignableFrom( DataSource.class ) ) {
			return (T) dataSource;
		}
		else {
			throw new UnknownUnwrapTypeException( unwrapType );
		}
	}

	@Override
	public void configure(@Nonnull Map<String, Object> properties) {
		CONNECTION_INFO_LOGGER.configureConnectionPool( "c3p0" );

		final String jdbcDriverClass = extractSetting(
				properties,
				JdbcSettings.JAKARTA_JDBC_DRIVER,
				JdbcSettings.DRIVER,
				JdbcSettings.JPA_JDBC_DRIVER
		);
		final String jdbcUrl = extractSetting(
				properties,
				JdbcSettings.JAKARTA_JDBC_URL,
				JdbcSettings.URL,
				JdbcSettings.JPA_JDBC_URL

View on GitHub (pinned to fad1729dce)

Solutions

  1. Add the sort expression to the select list so the emulation can reference it by ordinal.
  2. Wrap the full join in a subquery and sort in the outer query, where plain expression ordering works.
  3. Select and then ignore the extra column in your result mapping (Object[] or projection DTO).
  4. Replace the full join with a hand-written union of left/right joins if the projection must stay narrow.

Example fix

// before: order by expression not selected -> error on MySQL
List<A> list = session.createQuery(
    "select a from A a full join a.items i order by i.label", A.class)
    .getResultList();

// after: select the sort expression too
List<Object[]> list = session.createQuery(
    "select a, i.label from A a full join a.items i order by i.label", Object[].class)
    .getResultList();
Defensive patterns

Strategy: try-catch

Validate before calling

// For string queries: every order-by expression must appear in the select clause
static boolean orderByCovered(String selectClause, List<String> orderByExprs) {
    return orderByExprs.stream().allMatch( selectClause::contains );
}

Try / catch

try {
    return em.createQuery(hql, type).getResultList();
} catch ( UnsupportedOperationException e ) {
    if ( e.getMessage() != null && e.getMessage().contains("order by expressions to be in the select list for union queries") ) {
        return em.createQuery(addSortKeysToSelect(hql), Object[].class).getResultList();
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/999f0e6e38b37ebd. Report an issue: GitHub.