hibernate/hibernate-orm · error · HibernateException

Owner alias [{ownerAlias}] is unknown for alias [{alias}]

Error message

Owner alias [{ownerAlias}] is unknown for alias [{alias}]

What it means

While processing a legacy native-query return mapping, ResultSetMappingProcessor.processFetchReturn resolves each fetch return's owner alias. If the owner alias was never declared among the query's returns (alias2Return does not contain it), Hibernate throws HibernateException — it has no entity/collection persister to fetch from for that alias.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/sql/internal/ResultSetMappingProcessor.java:544

		final String keyPrefix = "element.";
		for ( var element : propertyResults.entrySet() ) {
			final String path = element.getKey();
			if ( path.startsWith( keyPrefix ) ) {
				result.put( path.substring( keyPrefix.length() ),
						element.getValue() );
			}
		}
		return result;
	}

	private void processFetchReturn(NativeQuery.FetchReturn fetchReturn) {
		final String alias = fetchReturn.getTableAlias();
		if ( !alias2Persister.containsKey( alias ) && !alias2CollectionPersister.containsKey( alias ) ) {
			final String ownerAlias = fetchReturn.getOwnerAlias();

			// Make sure the owner alias is known...
			if ( !alias2Return.containsKey( ownerAlias ) ) {
				throw new HibernateException( "Owner alias [" + ownerAlias + "] is unknown for alias [" + alias + "]" );
			}

			// If this return's alias has not been processed yet, do so before further processing of this return
			if ( !alias2Persister.containsKey( ownerAlias ) ) {
				processReturn( alias2Return.get( ownerAlias ) );
			}

			final var ownerPersister = alias2Persister.get( ownerAlias );
			final String fetchableName = fetchReturn.getFetchable().getFetchableName();
			final var returnType = ownerPersister.getPropertyType( fetchableName );
			if ( returnType instanceof CollectionType ) {
				addCollection(
						ownerPersister.getEntityName() + '.' + fetchableName,
						alias,
						emptyMap() //fetchReturn.getPropertyResultsMap()
				);
	//			collectionOwnerAliases.add( ownerAlias );
			}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Declare the owner return before the fetch: addEntity/addRoot/addJoin with the exact alias used as ownerAlias.
  2. Check the alias spelling and case — owner alias must exactly equal the declared table alias.
  3. Prefer addJoin("alias", "ownerAlias.property") which implies the owner, or migrate to @SqlResultSetMapping / resultClass-based mappings.

Example fix

// before
NativeQuery<?> q = session.createNativeQuery("select {m.*}, {k.*} from cat m join kitten k on k.mother_id = m.id");
q.addEntity("m", Cat.class);
q.addFetch("k", "mother", "kittens"); // owner alias "mother" never declared

// after
q.addEntity("m", Cat.class);
q.addJoin("k", "m.kittens");
Defensive patterns

Strategy: validation

Validate before calling

java.util.Set<String> declaredAliases = new java.util.HashSet<>();
// populate as you add returns: addEntity/addRoot/addJoin each register an alias
declaredAliases.add("m");

String ownerAlias = "m"; // alias you intend to pass to addFetch
if (!declaredAliases.contains(ownerAlias)) throw new IllegalArgumentException("Owner alias '" + ownerAlias + "' not declared — add it with addEntity/addJoin before addFetch");
query.addFetch("k", ownerAlias, "kittens");

Try / catch

try { ((org.hibernate.query.sql.spi.NativeQueryImplementor<?>) query).list(); } catch (org.hibernate.HibernateException e) { /* if 'Owner alias ... is unknown', verify declared aliases and mapping order */ throw e; }

Prevention

When it happens

Trigger: Programmatic legacy mapping: query.addFetch("k", "mother", "kittens") where no addEntity("mother", ...) / addRoot / addJoin declared the alias 'mother' earlier. Also hbm.xml <return-join>/<return-fetch> style mappings whose owner alias does not match any declared return alias (typo, case mismatch, or missing <return> entry).

Common situations: Migrating old Hibernate-native query mappings (addEntity/addJoin/addFetch) to Hibernate 6 where alias handling is stricter; typos or renamed aliases in hbm.xml mapping documents; returns added in the wrong order so the fetch is processed before its owner is registered.

Related errors


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