hibernate/hibernate-orm · error · InstantiationException

Cannot instantiate abstract class or interface

Error message

Cannot instantiate abstract class or interface

What it means

Thrown by EntityInstantiatorPojoStandard.instantiate() when Hibernate is asked to create an instance of an entity whose mapped class is abstract (or an interface). The instantiator deliberately skips constructor resolution for abstract classes (constructor = null when isAbstract()), so any runtime path that requires a concrete instance of the abstract mapped class itself - rather than one of its mapped subclasses - fails with this InstantiationException.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityInstantiatorPojoStandard.java:91

							null,
							null
					) );
		}
		return entity;

	}

	@Override
	public boolean isInstance(Object object) {
		return super.isInstance( object )
			// this one needed only for guessEntityMode()
			|| proxyInterface != null && proxyInterface.isInstance( object );
	}

	@Override
	public Object instantiate() {
		if ( isAbstract() ) {
			throw new InstantiationException( "Cannot instantiate abstract class or interface", getMappedPojoClass() );
		}
		else if ( constructor == null ) {
			throw new InstantiationException( "No default constructor for entity", getMappedPojoClass() );
		}
		else {
			try {
				return applyInterception( constructor.newInstance( (Object[]) null ) );
			}
			catch ( Exception e ) {
				throw new InstantiationException( "Could not instantiate entity", getMappedPojoClass(), e );
			}
		}
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. If the base type should never be instantiated, map it as @MappedSuperclass instead of @Entity
  2. Ensure every discriminator value stored in the database has a concrete @Entity subclass mapping
  3. Query/load concrete subclass types rather than the abstract root where instantiation is implied
  4. If the class must stay a mapped @Entity but is abstract, add a concrete subclass covering the rows in question

Example fix

// before
@Entity
@Inheritance(strategy = SINGLE_TABLE)
public abstract class Payment { ... }   // rows exist for unknown discriminator values

// after
@MappedSuperclass            // not an entity itself
public abstract class PaymentBase { ... }

@Entity
@Inheritance(strategy = SINGLE_TABLE)
public abstract class Payment extends PaymentBase { ... } // plus concrete CardPayment/CashPayment for every discriminator value
Defensive patterns

Strategy: validation

Validate before calling

// Startup guard: no mapped @Entity class should be abstract AND instantiable via root loads
for ( EntityType<?> t : emf.getMetamodel().getEntities() ) {
    if ( Modifier.isAbstract( t.getJavaType().getModifiers() ) && isRootEntity(t) && !hasMappedSubclassesForAllDiscriminators(t) ) {
        log.warn("Abstract mapped entity {} may receive unresolvable rows - consider @MappedSuperclass", t.getName());
    }
}

Type guard

static boolean isConcrete(Class<?> c) { return !Modifier.isAbstract(c.getModifiers()); }

Try / catch

try {
    return em.find(Payment.class, id);
}
catch ( org.hibernate.InstantiationException e ) {
    if ( "Cannot instantiate abstract class or interface".equals(e.getMessage()) ) {
        throw new IllegalStateException("Row resolves to abstract root " + e.getMessage() + " - map missing subclass or use @MappedSuperclass", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Loading a row whose discriminator/subclass resolution lands on the abstract root entity (e.g., a discriminator value mapped to the abstract class or missing subclass mappings); session.refresh/instantiate directly against an abstract entity name; polymorphic references (to-one associations typed as the abstract root) where the target resolves to the root entity itself.

Common situations: Mapping an abstract base class with @Entity and forgetting @MappedSuperclass; adding a new discriminator value in the database without mapping the corresponding subclass; queries like em.find(AbstractBase.class, id) on hierarchies where the row belongs to an unmapped/abstract type.

Related errors


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