hibernate/hibernate-orm · error · InstantiationException

Could not instantiate entity

Error message

Could not instantiate entity

What it means

The POJO indirecting instantiator creates embeddable instances by reordering ValueAccess values per the constructor-to-property index and calling constructor.newInstance(values). Any Exception from that call — constructor throws, wrong argument types, IllegalAccessException, InvocationTargetException — is wrapped into InstantiationException("Could not instantiate entity") naming the mapped class and carrying the original cause.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorPojoIndirecting.java:52

		}
		final var index = new int[componentNames.length];
		return EmbeddableHelper.resolveIndex( propertyNames, componentNames, index )
				? new EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index )
				: new EmbeddableInstantiatorPojoIndirecting( constructor, index );
	}

	@Override
	public Object instantiate(ValueAccess valuesAccess) {
		try {
			final var originalValues = valuesAccess.getValues();
			final var values = new Object[originalValues.length];
			for ( int i = 0; i < values.length; i++ ) {
				values[i] = originalValues[index[i]];
			}
			return constructor.newInstance( values );
		}
		catch ( Exception e ) {
			throw new InstantiationException( "Could not instantiate entity", getMappedPojoClass(), e );
		}
	}

	// Handles gaps, by leaving the value null for that index
	private static class EmbeddableInstantiatorPojoIndirectingWithGap extends EmbeddableInstantiatorPojoIndirecting {

		public EmbeddableInstantiatorPojoIndirectingWithGap(Constructor<?> constructor, int[] index) {
			super( constructor, index );
		}

		@Override
		public Object instantiate(ValueAccess valuesAccess) {
			try {
				final var originalValues = valuesAccess.getValues();
				final var values = new Object[index.length];
				for ( int i = 0; i < values.length; i++ ) {
					final int index = this.index[i];
					if ( index >= 0 ) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Read the cause chain (getCause()) — the real exception is the constructor's own failure, not this wrapper.
  2. Make the constructor tolerant of nulls during instantiation (defer validation to lifecycle callbacks or accept nullable params as primitives' defaults).
  3. Keep constructor parameter types aligned with the mapped property types after refactors.
  4. Ensure the constructor is accessible (public, or module opened to hibernate) so newInstance is legal.

Example fix

// before
public class Address {
    public Address(String city, String zip) {
        Objects.requireNonNull(city); // throws on null-row join instantiation
        ...
    }
}

// after
public class Address {
    public Address(String city, String zip) {
        this.city = city; // tolerate nulls; validate on persist if needed
        this.zip = zip;
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    OrderLine line = session.find(OrderLine.class, pk);
} catch (InstantiationException e) {
    if ("Could not instantiate entity".equals(e.getMessage())) {
        Throwable cause = e.getCause(); // the constructor's real exception — fix that first
        log.error("Embeddable constructor failed for {}", e.getClassName(), cause);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling instantiate directly or loading/persisting an entity whose embeddable uses constructor injection where: the constructor throws (NPE, validation), an argument type mismatches a parameter (e.g. property resolved as a different wrapper/enum), or the constructor is not accessible to Hibernate (private in a non-opened package/module).

Common situations: Embeddable constructors that validate arguments and reject nulls during LEFT JOIN / empty-composite creation; type drift after changing a property type without updating the constructor signature; Java modules (JPMS) not opening the embeddable package to Hibernate; Kotlin/Scala classes with constructors requiring non-null primitives receiving null during outer-join null-row instantiation.

Related errors


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