hibernate/hibernate-orm · error · HibernateException

Could not locate table which owns column [%s] referenced in

Error message

Could not locate table which owns column [%s] referenced in order-by mapping - %s

What it means

When Hibernate translates an @OrderBy on an association, it resolves each referenced column to the table that owns it using the hierarchy's subclass column closure. If no table in the hierarchy owns the column, this HibernateException is thrown, naming the column and the entity. The quoted-vs-unquoted comparison in the source also means quoting style must match the mapping exactly.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/persister/entity/JoinedSubclassEntityPersister.java:983

		}

		for ( int i = 0, max = subclassColumnClosure.length; i < max; i++ ) {
			final String subclassColumn = subclassColumnClosure[i];
			final boolean quoted =
					subclassColumn.startsWith( "\"" )
					&& subclassColumn.endsWith( "\"" );
			if ( quoted ) {
				if ( subclassColumn.equals( columnName ) ) {
					return subclassColumnNaturalOrderTableNumberClosure[i];
				}
			}
			else {
				if ( subclassColumn.equalsIgnoreCase( columnName ) ) {
					return subclassColumnNaturalOrderTableNumberClosure[i];
				}
			}
		}
		throw new HibernateException(
				"Could not locate table which owns column [" + columnName + "] referenced in order-by mapping - " + getEntityName()
		);
	}

	@Override
	public Object forceVersionIncrement(Object id, Object currentVersion, SharedSessionContractImplementor session) {
		final var superMappingType = getSuperMappingType();
		return superMappingType != null
				? superMappingType.getEntityPersister().forceVersionIncrement( id, currentVersion, session )
				: super.forceVersionIncrement( id, currentVersion, session );
	}

	@Override
	public Object forceVersionIncrement(
			Object id,
			Object currentVersion,
			boolean batching,
			SharedSessionContractImplementor session) throws HibernateException {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Order by a column owned by one of the hierarchy's mapped tables - verify the spelling against the target entity's @Column
  2. If the column lives only on a subclass, move it up the hierarchy or sort the collection in memory after loading
  3. Match the quoting exactly: if the @Column name is quoted, the @OrderBy fragment must use the same quoted form
  4. For formula-backed attributes, order by the underlying physical column or drop the mapping-level @OrderBy

Example fix

// before: column does not exist on the target entity's tables
@OneToMany(mappedBy = "order")
@OrderBy("customerLastName") // property maps to @Formula -> no owning table
List<OrderLine> lines;

// after: order by a real column
@OrderBy("lastName")
Defensive patterns

Strategy: validation

Validate before calling

// fail fast: every @OrderBy column must map to a column of the target entity
for (Field f : entityClass.getDeclaredFields()) {
    OrderBy ob = f.getAnnotation(OrderBy.class);
    if (ob != null && !ob.value().isEmpty()) {
        String col = ob.value().trim().split("\\s+")[0].replace("\"", "");
        assert targetColumnsContain(f, col) : "@OrderBy references unknown column " + col;
    }
}

Try / catch

try { session.createQuery(...).getResultList(); } catch (HibernateException e) { if (e.getMessage().contains("order-by mapping")) { /* fix the @OrderBy column against the target mapping */ } throw e; }

Prevention

When it happens

Trigger: @OrderBy("col") on @OneToMany/@ManyToMany/@ElementCollection where col is misspelled, is a @Formula attribute, lives only in a joined-subclass table not covered by the closure, or is quoted differently than the @Column mapping; also @OrderColumn/@OrderBy confusion (the value must be a column list, not an arbitrary expression).

Common situations: Ordering by a property name that maps to a formula; column renamed in a schema migration but not in the mapping; ordering by subclass-only columns in a JOINED hierarchy; quoted columns where the order-by uses the unquoted form.

Related errors


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