hibernate/hibernate-orm · error · AnnotationException

Could not instantiate comparator class

Error message

Could not instantiate comparator class '%s' for collection '%s'

What it means

Wraps any exception thrown while reflectively instantiating the comparator class declared via @Sort on a sorted collection. The comparator class was resolved but its no-arg constructor failed or is inaccessible.

Solutions

  1. Give the comparator class a public no-argument constructor and ensure it is on the classpath.
  2. Check the comparator class name in the mapping for typos.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Hibernate cannot instantiate a user-supplied class named in a mapping annotation.

Common situations: Custom AuxiliaryDatabaseObject or comparator classes that are missing from the classpath, non-public, abstract, or lack a usable no-arg constructor.


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

Appendix: source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java:1371

				collection.setOrderBy( sqlOrder.value() );
			}
		}

		final boolean isSorted = isSortedCollection || sorted;
		if ( isSorted && ordered ) {
			throw buildIllegalOrderAndSortCombination();
		}
		collection.setSorted( isSorted );
		instantiateComparator( collection, comparatorClass );
	}

	private void instantiateComparator(Collection collection, Class<? extends Comparator<?>> comparatorClass) {
		if ( comparatorClass != null ) {
			try {
				collection.setComparator( comparatorClass.newInstance() );
			}
			catch (Exception e) {
				throw new AnnotationException(
						String.format(
								"Could not instantiate comparator class '%s' for collection '%s'",
								comparatorClass.getName(),
								safeCollectionRole()
						),
						e
				);
			}
		}
	}

	private AnnotationException buildIllegalOrderCombination() {
		return new AnnotationException(
				String.format(
						Locale.ROOT,
						"Collection '%s' is annotated both '@%s' and '@%s'",
						safeCollectionRole(),
						jakarta.persistence.OrderBy.class.getName(),

View on GitHub (pinned to fad1729dce)