hibernate/hibernate-orm · error · IllegalStateException

Cannot instantiate class '{}' (it has no constructor with si

Error message

Cannot instantiate class '{}' (it has no constructor with signature {}, and not every argument has an alias)

What it means

Thrown for HQL dynamic class instantiation (`select new com.acme.Dto(...)`) when Hibernate cannot find a constructor of the target class whose parameter types match the selected arguments in order, and the only remaining strategy - alias-based bean injection into fields/setters - is unavailable because at least one argument has no alias. The message includes the signature Hibernate was looking for, derived from the resolved argument types.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationResultImpl.java:181

						.collect( toList() ),
				creationState.getSqlAstCreationContext()
						.getMappingMetamodel()
						.getTypeConfiguration()
		);
		if ( constructor != null ) {
			constructor.setAccessible( true );
			return new DynamicInstantiationAssemblerConstructorImpl<>( constructor, javaType, argumentReaders );
		}

		if ( LOG.isDebugEnabled() ) {
			LOG.debugf(
					"Could not locate appropriate constructor for dynamic instantiation of [%s]; attempting bean-injection instantiation",
					javaType.getTypeName()
			);
		}

		if ( !areAllArgumentsAliased) {
			throw new IllegalStateException(
					"Cannot instantiate class '" + javaType.getTypeName() + "'"
							+ " (it has no constructor with signature " + signature()
							+ ", and not every argument has an alias)"
			);
		}
		if ( !duplicatedAliases.isEmpty() ) {
			throw new IllegalStateException(
					"Cannot instantiate class '" + javaType.getTypeName() + "'"
							+ " (it has no constructor with signature " + signature()
							+ ", and has arguments with duplicate aliases ["
							+ StringHelper.join( ",", duplicatedAliases) + "])"
			);
		}

		return new DynamicInstantiationAssemblerInjectionImpl<>( javaType, argumentReaders );
	}

	private static Class<?> argumentClass(ArgumentReader<?> reader) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Add a constructor to the target class whose parameter types exactly match the selected expressions (order and types), e.g. `public PersonDto(Long id, String name)`
  2. Alternatively alias every argument (`e.id as id`) and expose settable fields or setters with matching names so the injection fallback can be used
  3. Verify the actual argument types by temporarily querying `Object[]` (`select e.id, e.name ...` with `Object[].class`) and compare against the constructor you provide

Example fix

// before
select new org.acme.PersonDto(e.id, e.name) from Person e   // PersonDto has no (Long,String) ctor
// after
public PersonDto(Long id, String name) { ... }   // matches the selection order and types
Defensive patterns

Strategy: validation

Validate before calling

// Discover the exact argument types Hibernate will resolve, then mirror them in the DTO constructor
List<Object[]> probe = session.createQuery("select e.id, e.name from Employee e", Object[].class).setMaxResults(1).getResultList();
if (!probe.isEmpty()) {
    for (Object v : probe.get(0)) System.out.println(v == null ? "null" : v.getClass().getName());
}

Try / catch

catch (IllegalStateException e) { if (e.getMessage().contains("no constructor with signature")) { /* fix DTO ctor per the printed signature */ } throw e; }

Prevention

When it happens

Trigger: `select new org.acme.PersonDto(e.id, e.name)` where PersonDto has no `(Long, String)` constructor and arguments are unaliased; entity attribute type changed (e.g. Date to LocalDate) so the previously matching DTO constructor no longer matches; selecting more arguments than any constructor accepts.

Common situations: DTO signature drift after entity refactors; adding a column to the select list without updating the DTO; primitive-vs-wrapper or java.util.Date-vs-temporal type mismatches between the selection and the constructor; upgrading Hibernate versions where argument type resolution changed subtly.

Related errors


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